Ruby on rails 4 Rails、嵌套资源、更新操作
我试图通过提交表单为每个评论更新一个简单的按钮。以下是我的查看代码:Ruby on rails 4 Rails、嵌套资源、更新操作,ruby-on-rails-4,form-for,nested-resources,Ruby On Rails 4,Form For,Nested Resources,我试图通过提交表单为每个评论更新一个简单的按钮。以下是我的查看代码: <% @comments.each do |comment| %> <%= form_for comment, url: article_comment_path(comment.article, comment), method: :patch do |f| %> <%= hidden_field_tag :update_time, Time.now %>
<% @comments.each do |comment| %>
<%= form_for comment, url: article_comment_path(comment.article, comment), method: :patch do |f| %>
<%= hidden_field_tag :update_time, Time.now %>
<%= f.submit "Confirm" %>
<% end %>
<% end %>
使用上面的代码,我得到以下错误:
缺少参数或值为空:comment
-该错误突出显示了您提交到文章注释路径的私有声明中的params.require行,但您的表单是针对文章的(就像在你的代码中你这里的问题很简单,看看你的表单,你没有任何
:note
,所以当你试图在你的params散列中要求:note
时,你会得到这个错误,因为在你的params散列中没有:note
键,你有两个选择:
private
def注释参数
参数要求(:注释)。允许(:日期,:注释)
终止
def注释参数最小值
参数要求(:注释)。允许(:日期)
结束
更新操作中有条件地使用它:
def update
@article = Article.friendly.find(params[:article_id])
@comment = @user.comments.find(params[:id])
if params[:comment][:note].present?
use_this_params = comment_params
else
use_this_params = comment_params_minimal
end
if @comment.update(use_this_params)
redirect_to @comments
else
render article_comments_path(@article)
end
end
另一种方法是使用params
hash直接更新您的注释,而不是使用comment\u params
soif params[:comment][:note]将它们列入白名单。显示?
以正常方式更新否则,只直接更新date
属性:params[:comment][:date
希望能对你有所帮助。你好,如果我的答案是有用的,请考虑选择它作为可接受的答案,这就是社区的工作方式……嗨,Im仍然在等你给我的答案打个招呼,我花了一些时间回答你…谢谢。
def update
debugger #<<<<<<<<<
@article = Article.friendly.find(params[:article_id])
@comment = @user.comments.find(params[:id])
if @comment.update(comment_params)
redirect_to @comments
else
render article_comments_path(@article)
end
end
params[:article][:comment]
def update
@article = Article.friendly.find(params[:article_id])
@comment = @user.comments.find(params[:id])
if params[:comment][:note].present?
use_this_params = comment_params
else
use_this_params = comment_params_minimal
end
if @comment.update(use_this_params)
redirect_to @comments
else
render article_comments_path(@article)
end
end