这与我的另一个问题是一致的;当我提交属于特定帖子的新评论时,试图获取正确的 post_id 。
_form.html.erb
<%= f.hidden_field :project_id, :value => params[:id] %>
<%= f.hidden_field :post_id, :value => params[:id].post_id %>
<%= f.hidden_field :user_id, :value => current_user.id %>
如果没有看到更多代码,很难确切地知道你在做什么,但我的猜测是 @post 实例变量已使用设置@post = Post.find(params[:id])
在控制器中
# this is not needed, on the create, get it from the post?
<%= f.hidden_field :project_id, :value => params[:id] %>
# if you do want to pass it, guessing something like this
<%= f.hidden_field :project_id, :value => @post.project_id %>
# pass the post id to the create action
<%= f.hidden_field :post_id, :value => @post.id %>
# if the comment has a project_id
# @comment.project = @post.project ?
# do not send this in hidden field, get the value in your controller
# otherwise the user can change this value to another user when submitting the form
<%= f.hidden_field :user_id, :value => current_user.id %>
您可能还想考虑在注释的情况下使用嵌套路由?
我建议阅读 Rails 指南中有关视图助手的内容:http://guides.rubyonrails.org/index.html http://guides.rubyonrails.org/index.html
the http://guides.rubyonrails.org/getting_started.html http://guides.rubyonrails.org/getting_started.html页面实际上有带有评论的帖子的代码示例
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)