簡體   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