简体   繁体   English

在Rails 4中更新属性

[英]Updating attributes in Rails 4

I am looking for some clarification on permitted params and updating a record. 我正在寻找有关允许的参数的澄清,并更新记录。

I understand that whitelisting attributes is now done in the controller and i create a provate method at the bottom of my controller for example 我知道白名单属性现在已在控制器中完成,例如,我在控制器底部创建了一个证明方法

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

if i want to be able to access these attributes within my create action i can pass them through like so 如果我希望能够在我的create动作中访问这些属性,则可以像这样传递它们

@gallery = Gallery.new(gallery_params)

I am however a bit stuck on how to permit the same params through my update method 但是我对如何通过我的更新方法允许相同的参数有些困惑

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

I have tried 我努力了

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

but that doesn't work 但这不起作用

Any help appreciated 任何帮助表示赞赏

Thanks 谢谢

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

if @gallery.update(gallery_params)

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

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

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