ruby-on-rails - 使用 update_attributes 时 ruby on rails 中的 ForbiddenAttributesError
问题描述
我正在创建简单的 ruby 博客应用程序我正在更新一篇文章,但它显示了这种类型的错误 错误
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post
puts "******************dd*********************"
puts params[:post]
puts "**************************************"
@post.update_attributes(params[:post])
redirect_to post_path, :notice => "you post has been updated"
else
render "edit"
end
end
但是当我使用这个
@post.update_attributes({"title"=>"2nd", "body"=>"this is second post body", "category_id"=>"2"})
这是工作
解决方案
您应该使用强参数,Rails 会出于安全原因阻止您分配来自外部世界的未过滤参数。
def update
# ...
@post.update_attributes(post_params)
# ...
end
# ...
private
def post_params
params.require(:post).permit(:title, :body, :category_id)
end
推荐阅读
- sqlite - “SET”附近:语法错误,同时尝试设置隔离级别
- windows - 保存/保留在 Windows 10 主机上的 docker 容器中创建的 jupyter 笔记本
- angular - 使用 CustomValueAccessor 在自定义控件中重置反应形式 FormControl / AbstractControl
- ubuntu - 遵循文档时无法从源代码安装 CKAN 2.8.3
- oauth-2.0 - IdentityServer4 API 资源与 API 范围中的用户声明是什么
- .net-core - aspnet core 3 中的混合表单和 Windows 身份验证
- asp.net-mvc - 带有制表符和 Web API 的 HTTP POST
- swift - 快速执行applescript时出现“应用程序未运行。(错误-600)”
- powershell - 从两个列表中提取数据比较并将其导出到一个
- linker - 如何在引导加载程序和应用程序之间设置共享库?