I have a status update, and comment db table.
A user has many status updates, and a status update has many comments. Similar to facebook, When a users friend goes to the users feed page (show page), they should be able to comment on the users status updates.
I’m having issues saving a users friends comment.. my code is below.. I think it has something to do with the Comments Controller, Create method, “@comment = @statusupdate.comments.build(params[:comment])”
any guidance is much appreciated! thanks!
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
@statusupdates = @user.statusupdates.paginate(:page => params[:page], :per_page => 25)
@statusupdate = Statusupdate.new
@comment = Comment.new
end
end
show.html.erb
<% form_for @statusupdate do |f| %>
<%= f.error_messages %>
<div class="field">
<%= f.text_field :content %>
</div>
<% @statusupdates.each do |s| %>
<%= s.content %><br />
<% form_for @comment do |f| %>
<%= f.error_messages %>
<div class="field">
<%= f.text_field :comment %>
</div>
<div class="field">
<%= f.hidden_field :user_id, :value => current_user.id %>
</div>
<div class="actions">
<%= f.submit "Submit" %>
</div>
<br><br>
<% end %>
<% end %>
class CommentsController < ApplicationController
def create
@comment = @statusupdate.comments.build(params[:comment])
if @comment.save
flash[:success] = "Comment created!"
redirect_to root_path
else
@feed_items = []
render 'pages/home'
end
end
end
show.html.erb- I changed the top line of the status update comment form to:
I don’t exactly know what’s going on here but it worked for me~ hope it helps someone out~