Reputation: 347
I've created a newsfeed like feature on my page, where it shows all posts created by the people you follow. Much like Facebook, I would like users to be able to comment straight from the newsfeed, underneath the post.
This is probably an easy fix, but I haven't been able to figure out the best way, or even how to get the post_id.
My current newsfeeds/index.html.erb looks like this:
<ul>
<% @activities.each do |activity| %>
<li> <%= image_tag(activity.owner.avatar.url(:thumb), height: "64", width: "64") %> <%= activity.owner.fullname %>
<ul>
<li><strong><%= activity.trackable.title %></strong></li>
<li><%= activity.trackable.content %></li>
<ul>
<li><%= render 'comments/form' %></li>
</ul>
</ul>
</li>
<% end %>
</ul>
The comments/_form.html.erb:
<%= form_for [@post, @comment] do |f| %>
<%= f.text_area :content %>
<%= f.submit "Add comment" %>
<% end %>
Then we have the controllers:
newsfeeds_controller.rb
def index
@activities = PublicActivity::Activity.order("created_at desc").where(owner_id: current_user.friend_ids, owner_type: "User")
@comment = Comment.new
end
comments_controller.rb
class CommentsController < ApplicationController
before_filter :load_post
def create
@comment = @post.comments.build(params[:comment])
@comment.user = current_user
if @comment.save
@comment.create_activity :create, owner: current_user
redirect_to @post, notice: "Comment was created."
else
render :new
end
end
....
def load_post
@post = Post.find(params[:post_id])
end
end
So my question is how do I fix it so that I store the post_id and that it finds it?
Upvotes: 0
Views: 280
Reputation: 146
From what I can see the comment form seems to do a post request to a nested route (i.e. /posts/:post_id/comments). The post_id would then be retrieved from the url in the CommentsController.
The current 'comments/_form.html.erb' partial requires the @post variable to be able to generate the right action url (@post variable doesn't seem to be set anywhere in this case).
To solve this you could pass 'post' as a local variable to the form partial. That way your form partial would be able to create the right url and your controller would have access to the post_id.
See also http://guides.rubyonrails.org/layouts_and_rendering.html#local-variables
newsfeeds/index.html.erb:
<ul>
<% @activities.each do |activity| %>
<li> <%= image_tag(activity.owner.avatar.url(:thumb), height: "64", width: "64") %> <%= activity.owner.fullname %>
<ul>
<li><strong><%= activity.trackable.title %></strong></li>
<li><%= activity.trackable.content %></li>
<ul>
<li><%= render 'comments/form', post: activity.trackable %></li>
</ul>
</ul>
</li>
<% end %>
</ul>
comments/_form.html.erb:
<%= form_for [post, @comment] do |f| %>
<%= f.text_area :content %>
<%= f.submit "Add comment" %>
<% end %>
(There may be cleaner ways to implement this)
To add a nested route, edit 'config/routes.rb'
resources :posts do
resources :comments
end
PS: When rendering the same form multiple times on the same page, the DOM id would be the same for all forms and their input fields. To avoid this you should probably set the namespace-option in the form_for call (see this question: Rails: Using form_for multiple times (DOM ids))
comments/_form.html.erb:
<%= form_for [post, @comment], namespace: post.id do |f| %>
<%= f.text_area :content %>
<%= f.submit "Add comment" %>
<% end %>
Upvotes: 1