简体   繁体   中英

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

@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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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