将数据从一个视图转移到轨道上的另一种视图



我是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)

用:

替换上述代码部分
<% @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><%=link_to book.book_name, book_path(book.id)%>
      </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><%=link_to book.book_name, book_path(book.id)%></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>

,如果您在BooksController中没有show操作,则使用以下代码进行:

class BooksController < ApplicationController
  def show
    @book=Book.find(params[:id])
  end
end

,如果您在上面编写代码,则必须在views/books文件夹中使用以下代码中的show.html.erb进行命名。

<p>Name:<%=@book.book_name%></p>
<br>
<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>

您可以使用

<%= link_to book.book_name, shop_path(book) %>

如果您的show方法在shop_controller.rb中。

,这将起作用。

最新更新