簡體   English   中英

Rails活動存儲has_many_attachments允許參數發布

[英]Rails active storage has_many_attachments permit params issue

我想通過使用活動存儲來上載表單中的多個圖像,但是它給出了一個許可參數問題Unpermitted parameter: :avatars

params.require(:poi).permit(:title, :description,avatars: [])

_form.html.erb

<%= form.file_field :avatars %>

poi.rb

has_many_attached :avatars

在控制器中

@poi = Poi.new(poi_params)

respond_to do |format|
  if @poi.save
    format.html { redirect_to @poi, notice: 'Poi was successfully created.' }
    format.json { render :show, status: :created, location: @poi }
  else
    format.html { render :new }
    format.json { render json: @poi.errors, status: :unprocessable_entity }
  end
end

如何解決此問題?

請參考這里 您具有has_many_attached關系,您的代碼僅適用於has_one_attached關系。

對於has_one_attached

<%= form.file_field :avatars %>

對於has_many_attached

<%= form.file_field :avatars, multiple: true %>

對於直接上傳,

<%= form.file_field :attachments, multiple: true, direct_upload: true %>

添加multiple: true在文件字段中為multiple: true file_input應該如下所示:


<%= form.file_field :avatars, multiple: true %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM