开发者

Iterative display of objects in Rails

I find that when I iteratively render a collection of objects, say, comments, rails also lists the addresses of the objects. For example, my view may contain the following:

<h3>Comments</h3>
<% if @blogpost.comments.any? %>
  <%= @blogpost.comments.each do |comment| %>
    <%= render :partial => "comment", :locals => {:comme开发者_C百科nt => comment} %>
  <% end %>  
<% end %>

The view often shows this:

<h3>Comments</h3> 
  <p>comment #2</p> 
  <p class="post-info"> >> Example User,  about 1 hour ago. </p> 

  <p>this is user 1's comment on user 5's article</p> 
  <p class="post-info"> >> Example User,  2 days ago. </p> 

#&lt;Comment:0xb6f91968&gt;#&lt;Comment:0xb6f9016c&gt;

As you can see, there are a couple of address listings for the objects, which I would prefer not to have in the view. Is there a way to suppress this output? Thanks in advance for your time!


remove = from <%= @blogpost.comments.each do |comment| %>

when we add = in <% %> rails will execute erb code and render the output in html.


Also, you don't really need all of this code.
You can left only these two lines to render 'comment' partial for @blogpost.comments collection:

<h3>Comments</h3>
<%= render @blogpost.comments %>

and Rails will do the trick.
http://api.rubyonrails.org/classes/ActionView/Partials.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜