在Rails中将数据从一个视图传输到另一个视图

时间:2017-05-02 11:53:12

标签: ruby-on-rails ruby model-view-controller

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

2 个答案:

答案 0 :(得分:1)

将上述代码部分替换为:

<% @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>

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

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

如果您编写上面的代码,那么您必须使用以下代码在show.html.erb文件夹中创建一个名为views/books的视图。

<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>

答案 1 :(得分:0)

您可以使用

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

仅当show方法位于shop_controller.rb

时才会有效