Reputation: 1065
I have three Models I'm working with. Goal, Comment, and Notation. Goals have many comments and comments have many notations (smaller comments, very much like the comments on the answers here).
I'm trying to display the notations on each comment by appending it with
<%= render 'notations/notations' %>
This partial loops through each notation to display it:
<% @notations.each do |notation| %>
<%= notation.content %><br />
Posted <%= time_ago_in_words(notation.created_at) %> ago by
<%= link_to notation.user.username, user_path(notation.user) %>
<% end %>
My problem is that these notations appear on the comments, which appear on the Goal show page, where there is already a new comment form. Take a look.
Goal controller:
def show
@comments = Goal.comments.order("created_at DESC")
@comment = Comment.new
@notations = @comment.notations
end
The @notations = @comment.notations
doesn't work because I already have @comment reserved for the new one. I've also tried @notations = Goal.comment.notations
but that gives a no method error for comment. My question is how can I get at the comments being displayed in order to show their notations as well?
Upvotes: 0
Views: 50
Reputation: 26768
The answer here seems to be what you're looking for. There is a way to pass a local (not instance) variable to the partial. That way you can use comment
and not worry about the collission with @comment
.
Here's an example:
<!-- on goal show page -->
<% @comments.each do |comment| %>
<!-- show the comment content then show the partial -->
<%= render 'notations/notations', comment: comment %>
<% end %>
<!-- in partial, you now have access to comment -->
<% notations = comment.notations %>
<% notations.each do |notation %>
<!-- show the notation -->
<% end %>
Upvotes: 1