使用 ActiveAdmin + CarrierWave 上传多张照片
Multiple Photos Upload Using ActiveAdmin + CarrierWave
我正在尝试使用 carrierwave 和 activeadmin 上传多张照片 gem。但问题是 Rails 说有 Unpermitted parameter: photos
,即使我已经将照片添加到 activeadmin gem.
的 permit_params
#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设置的。
非常感谢任何帮助,谢谢。
抱歉回复晚了:).
希望对您有所帮助。
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.file_field :photos, multiple: true
end
f.actions
end
输入法中as: :file(第二个参数)真的可以上传多个文件吗?
我检查的是在第二个参数中指定为::file 不允许输入多个图像。它在 html 文件标签下方生成,您正在尝试将照片属性作为数组作为白名单参数获取。
<input id="photos" type="file" name="apartment_post[photos]">
rails file_field 提供了一种简便方法。 file_field 辅助方法 returns 一个 html 输入文件标签。尝试 file_field。
更多细节
https://apidock.com/rails/ActionView/Helpers/FormHelper/file_field
我不确定这是否会导致错误,但可能是原因。
进行上述更改后。如果您仍然遇到问题。您可以通过覆盖创建操作或将参数列入白名单的函数来使用调试器检查参数,如下所示。
controller do
def create
aparment_params = params.require(:apartment_post).permit(:title,
{ photos: [] })
// here you can use your debugger to check the params.
end
end
permitted_params函数returns白名单参数也请检查允许的参数。
https://activeadmin.info/2-resource-customization.html
并且.. 如果您仍然遇到问题,请告诉我。我会尽力帮助你 :)。祝你好运。
我正在尝试使用 carrierwave 和 activeadmin 上传多张照片 gem。但问题是 Rails 说有 Unpermitted parameter: photos
,即使我已经将照片添加到 activeadmin gem.
permit_params
#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设置的。
非常感谢任何帮助,谢谢。
抱歉回复晚了:).
希望对您有所帮助。
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.file_field :photos, multiple: true
end
f.actions
end
输入法中as: :file(第二个参数)真的可以上传多个文件吗?
我检查的是在第二个参数中指定为::file 不允许输入多个图像。它在 html 文件标签下方生成,您正在尝试将照片属性作为数组作为白名单参数获取。
<input id="photos" type="file" name="apartment_post[photos]">
rails file_field 提供了一种简便方法。 file_field 辅助方法 returns 一个 html 输入文件标签。尝试 file_field。 更多细节 https://apidock.com/rails/ActionView/Helpers/FormHelper/file_field
我不确定这是否会导致错误,但可能是原因。
进行上述更改后。如果您仍然遇到问题。您可以通过覆盖创建操作或将参数列入白名单的函数来使用调试器检查参数,如下所示。
controller do
def create
aparment_params = params.require(:apartment_post).permit(:title,
{ photos: [] })
// here you can use your debugger to check the params.
end
end
permitted_params函数returns白名单参数也请检查允许的参数。 https://activeadmin.info/2-resource-customization.html
并且.. 如果您仍然遇到问题,请告诉我。我会尽力帮助你 :)。祝你好运。