[英]update_attributes the data in ruby on rails
我正在使用ruby on rails,我正在尝试更新我的数据
def update
@post = Post.find(params[:id])
if @post.update_attributes(params[:post])
redirect_to post_path,:notice =>"post has been updated"
else
render "edit"
end
end
它没有更新,错误来自: -
PostsController#update中的ActiveModel :: ForbiddenAttributesError
帮我 !
你不能只根据params [:post]更新值,你必须使用强参数将它们列入白名单。
在控制器中基本上是白色的post params
def post_params
params.require(:post).permit(:title, :description)
end
然后在控制器中
def update
@post = Post.find(params[:id])
if @post.update_attributes(post_params)
redirect_to post_path,:notice =>"post has been updated"
else
render "edit"
end
end
private
def post_params
params.require(:post).permit(:title, :description)
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.