调用与部分关联的方法 rails
Call a method associated with partial rails
在我的控制器中,我有这个方法可以返回特定书籍的列表 controller/books_controller
def postings
@books = Book.postings(current_user.id).order("created_at ASC")
render :partial =>'postings'
end
我在 views/books/_postings.html.erb
中有一部分
<div class="container">
<h4>My Partial Postings</h4>
<% @books.each do |book| %>
<div class="row">
<div class="col-sm-2"><img src="<%= book.image_path %>" alt="..." class="img-responsive"/></div>
<div class="col-sm-10">
<h4 class="nomargin"><%= book.title %></h4>
<p><%= book.description %></p>
<p>Quantity: <%= book.quantity %></p>
<p>Available for: <%= book.sale_type %></p>
</div>
</div>
<hr>
<% end %>
</div>
在我的路线中:
resources :books do
collection do
get 'postings'
end
end
在运行条佣金路线上:
postings_books GET /books/postings(.:format) books#postings
当我使用 localhost:3000/books/postings 时,我得到了所需的部分书籍显示列表。但是当我想从其他视图(例如 localhost:3000/dashboard/index:
<div class="tab-pane" id="postings">
<%= render 'books/postings', :collection => @books %>
</div>
我收到以下错误:
Showing /home/swati/867/WorkSpace2/assignment_3/app/views/books/_postings.html.erb where line #4 raised:
undefined method `each' for nil:NilClass
Extracted source (around line #4):
<div class="container">
<h4>My Partial Postings</h4>
<% @books.each do |book| %>
<div class="row">
<div class="col-sm-2"><img src="<%= book.image_path %>" alt="..." class="img-responsive"/></div>
<div class="col-sm-10">
我知道 render 只是渲染部分,而没有调用我在 books_controller 中的方法帖子,我的部分无法访问 @books ,它是 nil。如何解决这个问题?
当您访问 dashboard/index
时,您实际上是在调用 dashboards_controller
的 index
方法。如果您想在那里呈现 books/postings
部分内容,您还需要在该控制器中添加图书列表 (@books
)。
更多信息在这里:http://guides.rubyonrails.org/action_controller_overview.html
在我的控制器中,我有这个方法可以返回特定书籍的列表 controller/books_controller
def postings
@books = Book.postings(current_user.id).order("created_at ASC")
render :partial =>'postings'
end
我在 views/books/_postings.html.erb
中有一部分<div class="container">
<h4>My Partial Postings</h4>
<% @books.each do |book| %>
<div class="row">
<div class="col-sm-2"><img src="<%= book.image_path %>" alt="..." class="img-responsive"/></div>
<div class="col-sm-10">
<h4 class="nomargin"><%= book.title %></h4>
<p><%= book.description %></p>
<p>Quantity: <%= book.quantity %></p>
<p>Available for: <%= book.sale_type %></p>
</div>
</div>
<hr>
<% end %>
</div>
在我的路线中:
resources :books do
collection do
get 'postings'
end
end
在运行条佣金路线上:
postings_books GET /books/postings(.:format) books#postings
当我使用 localhost:3000/books/postings 时,我得到了所需的部分书籍显示列表。但是当我想从其他视图(例如 localhost:3000/dashboard/index:
<div class="tab-pane" id="postings">
<%= render 'books/postings', :collection => @books %>
</div>
我收到以下错误:
Showing /home/swati/867/WorkSpace2/assignment_3/app/views/books/_postings.html.erb where line #4 raised:
undefined method `each' for nil:NilClass
Extracted source (around line #4):
<div class="container">
<h4>My Partial Postings</h4>
<% @books.each do |book| %>
<div class="row">
<div class="col-sm-2"><img src="<%= book.image_path %>" alt="..." class="img-responsive"/></div>
<div class="col-sm-10">
我知道 render 只是渲染部分,而没有调用我在 books_controller 中的方法帖子,我的部分无法访问 @books ,它是 nil。如何解决这个问题?
当您访问 dashboard/index
时,您实际上是在调用 dashboards_controller
的 index
方法。如果您想在那里呈现 books/postings
部分内容,您还需要在该控制器中添加图书列表 (@books
)。
更多信息在这里:http://guides.rubyonrails.org/action_controller_overview.html