Micropost's Comments on Users Page (Ruby on Rails)

Micropost's comments on users page (Ruby on Rails)

Try passing in the current micropost to the comment partial

<%= render 'shared/comment_form', micropost: micropost %>

Then add the micropost to the comment form_for call

<%= form_for([micropost, @comment]) do |f| %>

Make sure your routes are nested

# in routes.rb
resources :microposts do
resources :comments
end

Then build the comment off of the micropost in the CommentsController

def create
@micropost = Micropost.find(params[:micropost_id])
@comment = @micropost.comments.build(params[:comment])
...
end

Displaying microposts' comments for user

Change <%= render @comments%> to <%= render micropost.comments %> to loop through the specific post's comments instead of using generic comment loop.

Comments on Post model will not render, no error message displayed

Your has_many and belongs_to relationships appear to be set up correctly. This means you can rely upon rails to do most of the heavy lifting in your controller and views.

User Controller#show becomes...

def show
@user = User.find(params[:id])
end

views/users/show.html.erb becomes...

<% @user.microposts.each do |micropost| %>
<%= link_to gravatar_for(@user, size: 15), @user %>
<%= link_to @user.name, @user %>
<!-- You have access to micropost here as well, you could still do micropost.user, micropost.user.name instead of @user -->

<% micropost.comments.each do |comment| %>
<%= comment.comment_content %>
<% end %>
<% end %>

Rails How to append comments to a collection of microposts without page reload

Could you try the following :

In your create.js.erb :

$("#comments_for_#{@comment.micropost_id}%>").html("<%= escape_javascript(render('comments/comment')) %>");

I suspect something is wrong with your jquery selectors, and you can achieve what you want more easily.

PS : You should not rely on instance variables in your partials. Instead, pass your instance vars to partials via locals. Otherwise your partials cannot be reused easily.

How to associate the comment form with the correct post

just add a hidden field for the micropost_id

<%= form_for(@comment) do |f| %>
<%= f.hidden_field :micropost_id, value: @micropost.id %>
<div id="comment_field">
<%= f.text_field :content, placeholder: "Say Something..." %>
</div>
<% end %>

UPDATE: passing micropost_id without any changes to the controller

Based on your comments controller, you're finding micropost based on params[:id] which is missing when you submit the form. The code below fixes that. However, I suggest you look at nested resources which will make the controller code prettier and more slick

<%= form_for @comment do |f| %>
<%= hidden_field_tag :id, @micropost.id %>
<div id="comment_field">
<%= f.text_field :content, placeholder: "Say Something..." %>
</div>
<% end %>

or update the action of the form

<%= form_for @comment, url: comments_path(id: @micropost.id) do |f| %>
<div id="comment_field">
<%= f.text_field :content, placeholder: "Say Something..." %>
</div>
<% end %>

UPDATE: with edits to the comment controller

# view
<%= form_for @comment do |f| %>
<%= hidden_field_tag :micropost_id, @micropost.id %>
<div id="comment_field">
<%= f.text_field :content, placeholder: "Say Something..." %>
</div>
<% end %>

# comments_controller.rb

def create
@micropost = Micropost.find params[:micropost_id]
@comment = current_user.comments.build
@comment.micropost = @micropost
@comment.save
end


Related Topics



Leave a reply



Submit