保存与状态更新相关的评论对象.build ruby
我有一个状态更新,并评论数据库表。
一个用户有很多状态更新,一个状态更新有很多评论。与facebook类似,当用户朋友进入用户提要页面(显示页面)时,他们应该能够对用户状态更新发表评论。
我在保存用户朋友评论时遇到问题..我的代码如下..我认为它与评论控制器,创建方法,“@comment = @statusupdate.comments.build(params[:comment]) 有关“
非常感谢任何指导!谢谢!
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查表单的 html 以确保其正确。另请参阅哪些参数被发送到创建操作。
我看到的主要内容是状态更新和注释的表单是嵌套的,并且都使用块参数 f。这可能会导致事情变得非常奇怪(特别是因为 ruby 1.8 和 1.9 之间块参数的范围不同)。看起来您实际上并不希望嵌套表单。您还应该修复 html 中的缩进。
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-我将状态更新评论表单的第一行更改为:
我不完全知道这里发生了什么,但它对我有用〜希望它可以帮助别人〜
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~