0

In page1, I use code A+B+C

In page2, I use code B+C

So when I make a partial, I realy have no idea in how to deal with this.

For example, In a Post-Comment system. I want to show @comments in 2 different pages. In the comment index page, We show the post it belongs to. And in the post show page, We only have to show the comments content.(Since there is no need to show the comment.post again)

    #Comment Index Page
    <% @comments.each do |comment| %>
        <%=  comment.post %>
        <%=  comment.author %>
        <%=  comment.content %>
    <% end %>

..

    #Post Show Page
    <% @comments.each do |comment| %>
        <%=  comment.author %>
        <%=  comment.content %>
    <% end %>

So, how do I make a partial to reuse the code? Perhaps like this? But this there more elegant way of doing this?

    #Comment Index Page
    <% @comments.each do |comment| %>
        <%=  comment.post %>
        <%= render comment %>
    <% end %>

    #Post Show Page
    <% @comments.each do |comment| %>
        <%= render comment %>

<% end %>

Updated: I adopt the local variable approach, and update my code like:

    # partial

    <% if include_topic %>
        <div class="Topic">
          <h5><%= link_to "#{comment.topic.content}", comment.topic %></h5>
        </div>
    <% end %>
    #Index
    <%= render @comments, :locals => {:include_topic => true } %>

But I get undefined local variable or method `include_topic' for #<# I just find nowhere to debug this issue

ZK Zhao
  • 19,885
  • 47
  • 132
  • 206

2 Answers2

0

Your partial:

<%=  comment.post if include_post %>
<%=  comment.author %>
<%=  comment.content %>

your code:

#index page
<%= render :partial => "partial_path", :collection => @comments, :as => :comment, :locals => {:include_post => true } %>

#show page
<%= render :partial => "partial_path", :collection => @comments, :as => :comment, :locals => {:include_post => false } %>

Syntax could be much shorter but it depends whether or not you stick to rails conventions see doc.

Sidenote: I don't like 1.8.7 syntax

apneadiving
  • 114,565
  • 26
  • 219
  • 213
  • <%= comment.post if include_post %> should be <%= comment.post if :include_post %> ? – ZK Zhao Mar 21 '13 at 09:09
  • mmm, ok, the short partial helper doesn;t let you pass local variables, you have to use the verbose line of code – apneadiving Mar 21 '13 at 09:26
  • Fixed it! Find solution in http://stackoverflow.com/questions/4402556/rails-confused-about-syntax-for-passing-locals-to-partials. Your code should work fine( but it still fails, and I really don't know why). And I use codes like `<%= render @comments, include_topic: false %>`, and this works – ZK Zhao Mar 21 '13 at 09:37
0

In the partial,

<% comments.each do |comment| %>
  <%=  comment.post if params[:controller] == "comments" %>
  <%=  comment.author %>
  <%=  comment.content %>
<% end %>

and now render this partial in both comments/index and posts/show pages by specifying the comments as a local variable.

Sagar Bommidi
  • 1,409
  • 8
  • 12