繁体   English   中英

在Rails 4中更新属性

[英]Updating attributes in Rails 4

我正在寻找有关允许的参数的澄清,并更新记录。

我知道白名单属性现在已在控制器中完成,例如,我在控制器底部创建了一个证明方法

private
def gallery_params
  params.require(:gallery).permit(:id, :title, :overview, :category_id, gallery_images_attributes: [:id, :gallery_id, :gallery_category_id, :photo, :_destroy])
end

如果我希望能够在我的create动作中访问这些属性,则可以像这样传递它们

@gallery = Gallery.new(gallery_params)

但是我对如何通过我的更新方法允许相同的参数有些困惑

def update
@gallery = Gallery.find(params[:id])
if @gallery.update_attributes(params[:gallery])
  redirect_to root_path, notice: 'Successfully updated Gallery'
else
  render action: 'edit'
end
end

我努力了

@gallery.update_attributes(params[:gallery].permit(gallery_params))

但这不起作用

任何帮助表示赞赏

谢谢

您应该只使用您的gallery_params方法:

if @gallery.update(gallery_params)

无需为单个属性单独授予权限,只需使用gallery_params

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM