Reputation: 11
I have Post model, and loop for display Post in the index page. I have Comment which belongs to Post, so In the show Post page I have used loop <% @comments.each do |comment| %>
<%= comment.body %>
<% end %>
. But the post has a Response, so when I write "loop response in the loop comment" after clicking Add Response
I have an alert
The response has successfully added
but it doesn't show under the Comment, i think that it's broken loop:
My code in the post#show
<% @comments.each do |comment| %>
<p> Author: <%= comment.user.nickname %></p>
<p><%= comment.body %></p>
<%= link_to "Add a response to comment!",
new_post_comment_response_path(@post, comment) %>
<% @responses.each do |response| %>
<%= response.body_response %>
<% end %>
<% end %>
Is it wrong?
Upvotes: 0
Views: 111
Reputation: 941
I think the way you want is that you will have a post, which can have many comments and there will be responses to the comments.
This is what should be there in your models:
Post.rb --> :has_many comments
Comment.rb --> :belongs_to post
Comment.rb --> :has_many responses
Response.rb --> :belongs_to comment
Post this, you can do the following:
@post = Post.find(:id)
@comments = @post.comments
@comments.each do |comment|
puts comment.info
puts comment.author
@responses = comment.responses
@responses.each.do |response|
// Considering the response resource has attributes info, likes, unlikes
puts response.info
puts response.likes
puts response.unlikes
end
end
Upvotes: 0
Reputation: 36860
You can't do @responses.each do |response|
because you haven't defined a @responses
instance variable in your controller... and you can't do so because each responses
collection is different for each comment
Assuming you have a has_many :responses
in your Comment class you can do...
<% comment.responses.each do |response| %>
Upvotes: 2