简体   繁体   English

ruby-on-rails:在列表中的每个项目上呈现部分内容

[英]ruby-on-rails: rendering partials on each item in a list

I have the following in a view (.html.erb) : 我在视图(.html.erb)中有以下内容:

<% @posts = GetAllPostsFunctions %>   (removed for berivity)

<% @posts.each do |post| %>
<%=  post.title %>

<%= render :partial => "posts/post_show" %>
<% end %>

the posts_show partial has the following: posts_show部分具有以下内容:

....
<td><%=h @post.title %> </td>

But I get the following error 但是我收到以下错误

You have a nil object when you didn't expect it!
The error occurred while evaluating nil.title

Any ideas? 有任何想法吗?

You can also simply things by using the :collection for render :partial. 您还可以使用:collection for render:partial来简单地处理事情。 Which pass each item in the value for :collection to a local variable sharing the name of your partial. 将值中的每个项目传递给:集合到共享部分名称的局部变量。

<% @posts = GetAllPostsFunctions %>   (removed for berivity)

<%= render :partial => "posts/post_show", :collection => @posts %>

In this case, Rails will render post_show for each item in @posts with the local variable post_show set to the current item. 在这种情况下,Rails将为@posts中的每个项目渲染post_show,并将局部变量post_show设置为当前项目。 It also provides handy counter methods. 它还提供方便的计数器方法。

Successfully using this approach would require renaming the app/views/posts/_post_show.html.erb partial to app/views/posts/_post.html.erb to or changing every occurance of post in your partial to post_show. 成功使用此方法需要将app / views / posts / _post_show.html.erb部分重命名为app / views / posts / _post.html.erb或将部分帖子中的每个帖子更改为post_show。 If you renamed the partial to the conventional _post.html.erb which will then allow you to simply do: 如果你将partial重命名为传统的_post.html.erb ,那么你可以简单地执行:

<%= render :partial => @posts %>

Which will render the partial for every single post in the @posts variable. 这将为@posts变量中的每个帖子呈现部分。

Since the post variable in the each loop is a locale variable you have to make it available to the partial: 由于每个循环中的post变量是一个locale变量,因此必须使其可用于partial:

<%= render :partial => "posts/post_show", :locals => {:post => post} %>

You can then access the title through the local variable post: 然后,您可以通过本地变量帖子访问标题:

<td><%=h post.title %> </td>

You could also simplify the whole thing by rendering the posts as collection. 您还可以通过将帖子呈现为集合来简化整个过程。 Take a look at the Rails documentation for more information: 有关更多信息,请查看Rails文档:

http://api.rubyonrails.org/classes/ActionController/Base.html#M000658 http://api.rubyonrails.org/classes/ActionController/Base.html#M000658

It doesn't look like you're setting the @post of the partial, so when it goes to evaluate the partial it gets a null reference. 看起来你没有设置partial的@post,所以当它去评估partial时,它会得到一个null引用。

Alternately, ensure that your post fetching functions are actually returning something 或者,确保您的帖子提取功能实际上返回了一些内容

I'm not positive, but I think in the partial you have to do post.title not @post.title 我不是肯定的,但我觉得你要做的部分是post.title而不是@post.title

Sorry if I misunderstood you, I'm new to rails. 对不起,如果我误解了你,我是铁杆新手。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM