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 bel开发者_运维百科ow.. 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
Check the html of the form to make sure its right. Also see what parameters are getting sent to the create action.
The main thing I see is that the forms for the status update and the comments are nested, and both use the block parameter f. This could cause things to get very strange (especially since the scoping of block parameters differs between ruby 1.8 and 1.9). It also seems like you don't actually want the forms nested. You should also probably fix the indentation in your html.
show.html.erb- I changed the top line of the status update comment form to:
<% form_for (s, s.comments.build) do |f| %>
...
class CommentsController < ApplicationController
def create
@statusupdate = statusupdate.find(params[:statusupdate_id])
@comment = @statusupdate.comments.create(params[:comment])
...
I don't exactly know what's going on here but it worked for me~ hope it helps someone out~
精彩评论