carrierwaveとactiveadmin gemを使用して複数の写真をアップロードしようとしています。しかし、問題は、私はpermit_params
activeadminの宝石に写真を追加したにもかかわらず、Unpermitted parameter: photos
があるとRailsは言う。ActiveAdmin + CarrierWaveを使用した複数の写真アップロード
#app/admin/apartment_post.rb
コード:
ActiveAdmin.register ApartmentPost do
permit_params :title, :max_stay, :bed_configuration, :number_of_guests,
:rate, :features, :description, photos: []
form(html: { multipart: true }) do |f|
f.inputs do
f.input :title
f.input :max_stay
f.input :bed_configuration
f.input :number_of_guests
f.input :rate
f.input :features
f.input :description
f.input :photos, as: :file, multiple: true
end
f.actions
end
end
#app/model/apartment_post.rb
コード:
class ApartmentPost < ApplicationRecord
mount_uploaders :photos, PhotoUploader
validates :title, :max_stay, :bed_configuration, :number_of_guests,
:rate, :features, :description, presence: true
end
schema.rb
コード:
create_table "apartment_posts", force: :cascade do |t|
t.integer "max_stay", null: false
t.string "bed_configuration", null: false
t.integer "number_of_guests", null: false
t.float "rate", null: false
t.string "features", null: false
t.string "description", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "title", null: false
t.json "photos"
end
photo_uploader.rb
ファイルcarrierwaveによって設定されたデフォルトです。
どのようなヘルプは非常にappriciated、ありがとうございます。
'photos'に空の配列を使う必要はありません。代わりに、 ':photos'は既存の' permit_params'に匹敵するように追加されるべきです。 JSONフィールドの処理に関する素晴らしいブログ投稿:https://lorefnon.me/2015/03/02/dealing-with-json-fields-in-active-admin.html – jdgray
しかし、carrierwaveは複数の画像をアップロードするには空の配列が必要です –
文書では、空の配列をハッシュで指す必要があると書かれています。 https:// github。com/carrierwaveuploader/carrierwave –