0

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?

3
  • Please provide the content of config/routes.rb Commented Jun 24, 2016 at 8:12
  • What is @responses? Commented Jun 24, 2016 at 8:13
  • Assuming the variables are assigned as expected the code looks correct. Commented Jun 24, 2016 at 8:40

2 Answers 2

2

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| %>
Sign up to request clarification or add additional context in comments.

7 Comments

So I just do that, and: Under the Comment I have all responses, not only ones that belong to comment id. My @responses in def show end method is: @responses = Response.where(:comment_id => params[:comment_id]).order("created_at DESC") I want to get responses which belong to id under the comment. How to get it? Thanks for all responses! I'm glad!
"comment belongs_to Post and has many:responses" So you (should) have a method "comment.responses" and you should use that. If you do @responses = Response.where(post_id: @post.id)... you're going to get all the responses for the post. As you say you're seeing. Did you try the code in my answer?
When I use @responses= Response.find(params[:review_id]) i have an error: "Couldn't find Response with 'id'="
WHY are you doing that? That's not what my answer suggested. My answer says change your view file to use <% comment.responses.each do |response| %> You don't need a @responses instance variable.
Sorry, I didn't understand you, so if i don't set @responses in Post controller, but only change the view file, the responses are not showing still. my view: <% comment.responses.each do |response| %> <%= comment.response.body %> <% end %> It's not working
|
0

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:

To show all comments of a particular post and the responses to them

@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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.