2017-04-17 5 views
1

carrierwave 및 activeadmin gem을 사용하여 여러 장의 사진을 업로드하려고합니다. 하지만 문제는 레일스가 Unpermitted parameter: photos이라고 말합니다. activeadmin gem의 permit_params에 사진을 추가 했음에도 불구하고 말입니다.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에 의해 설정된 기본값입니다.

도움이 매우 풍부합니다. 감사합니다.

+0

'photos'에 빈 배열을 사용할 필요는 없습니다. 대신,': photos'는 기존의'permit_params'에 필적 할만큼 추가되어야합니다. JSON 필드 처리에 대한 유용한 블로그 게시물 : https://lorefnon.me/2015/03/02/dealing-with-json-fields-in-active-admin.html – jdgray

+0

그러나 carrierwave는 여러 이미지를 업로드하려면 빈 배열이 필요합니다. –

+0

문서에서 해시 배열에서 빈 배열을 가리켜 야한다고 설명합니다. https : // github.co.kr/carrierwaveuploader/carrierwave. –

답변

2

죄송합니다. 회신 :).

희망이 도움이 될 것입니다.

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 

입력 방법에 : : 파일 (두 번째 매개 변수)을 사용하여 여러 파일을 정말로 업로드 할 수 있습니까?

내가 조사하는 내용은 다음과 같이 지정하는 것입니다. 두 번째 매개 변수의 파일은 여러 이미지를 입력 할 수 없습니다. 그것은 아래의 HTML 파일 태그를 생성하고 허용 된 매개 변수로 배열로 사진 속성을 얻으려고합니다.

<input id="photos" type="file" name="apartment_post[photos]"> 

레일이 file_field와 함께 제공하는 쉬운 방법이 있습니다. file_field 도우미 메서드는 HTML 입력 파일 태그를 반환합니다. file_field를 사용해보십시오. 자세한 내용은 https://apidock.com/rails/ActionView/Helpers/FormHelper/file_field

본인의 의도는 아니지만 가능한 원인은 무엇인지 알 수 없습니다.

위의 변경을 한 후에. 그래도 문제가 발생하면 아래의 params를 화이트리스트로 작성하는 함수 또는 create 액션을 오버라이드하여 디버거로 params를 확인할 수 있습니다.

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 

allowed_params 함수는 허용 된 매개 변수를 반환합니다. 허용되는 매개 변수도 확인하십시오. https://activeadmin.info/2-resource-customization.html

그리고 .. 아직도 문제가 발생하는 경우 알려주십시오. 나는 최선을 다해 당신을 도울 것입니다 :). 행운을 빕니다.