【发布时间】:2017-05-02 11:53:11
【问题描述】:
我是 Rails 新手,正在制作一个电子商务网站。 这就是我的观点的代码(这是代码的一部分,而不是全部):-
<% @thriller.each do |book|%>
<div class="thumbnail">
<img src="http://placehold.it/320x150" alt="">
<div class="caption">
<h4 class="pull-right"><%=book.mrp%></h4>
<h4><a href="#"><%=book.book_name%></a>
</h4>
</div>
<p>No. of pages:<%=book.pages%></p>
<br>
<p>In stock:<%=book.stock %></p>
<br>
<p>Synopsis:<%=book.synopsis %></p>
<br>
<p>Author:<%=book.author %></p>
<br>
<p>Publisher:<%=book.publisher %></p>
<br>
<p>Genre:<%=book.genre_name%></p>
<br>
<br>
<div class="ratings">
<p>
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star"></span>
</p>
</div>
</div>
<% end %>
</div>
<div class="col-sm-4 col-lg-4 col-md-4">
<% @classic.each do|book|%>
<div class="thumbnail">
<img src="http://placehold.it/320x150" alt="">
<div class="caption">
<h4 class="pull-right"><%=book.mrp%></h4>
<h4><a href="#"><%=book.book_name%></a></h4>
</div>
<p>No. of pages:<%=book.pages%></p>
<br>
<p>In stock:<%=book.stock %></p>
<br>
<p>Synopsis:<%=book.synopsis %></p>
<br>
<p>Author:<%=book.author %></p>
<br>
<p>Publisher:<%=book.publisher %></p>
<br>
<p>Genre:<%=book.genre_name%></p>
<br>
<br>
<div class="ratings">
<p>
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star"></span>
</p>
</div>
</div>
<% end %>
</div>
用于此的控制器具有以下代码:-
class ShopController < ApplicationController
def accessories
@ntbk=Notebook.all
@bkmk=Bookmark.all
end
def books
@thriller=Book.where(genre_id:1)
@classic=Book.where(genre_id:2)
@romance=Book.where(genre_id:3)
@selfhelp=Book.where(genre_id:4)
@autob=Book.where(genre_id:5)
end
end
我希望用户能够单击按钮并被重定向到显示该特定项目详细信息的页面。我该怎么做呢?
控制台日志:-
Processing by ShopController#view as
Book Load (0.2ms) SELECT "books".* FROM "books" WHERE "books"."book_id" IS NULL LIMIT ? [["LIMIT", 1]]
Rendering shop/view.html.erb within layouts/application
Rendered shop/view.html.erb within layouts/application (5.2ms)
Completed 500 Internal Server Error in 14ms (ActiveRecord: 0.2ms)
ActionView::Template::Error (undefined method `each' for nil:NilClass):
1: <% @book.each do|book|%>
2: <p>Name:<%=@book.book_name%></p>
3: <br>
4: <p>No. of pages:<%=@book.pages%></p>
app/views/shop/view.html.erb:1:in `_app_views_shop_view_html_erb__1842756845584902456_69979582064500'
Rendering /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout
Rendering /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
Rendered /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (4.7ms)
Rendering /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
Rendered /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.8ms)
Rendering /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
Rendered /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.7ms)
Rendered /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (23.0ms)
【问题讨论】:
-
搜索'link_to'标签以及如何使用它。
-
另请阅读模型资源路由,它为您提供了 7 个常用的默认 crud 请求。提示:你会在那里得到答案。
-
您发布的视图是索引操作?
-
@HarryBomrah 是的。
标签: ruby-on-rails ruby model-view-controller