Rails 4 | Unpermitted parameter: images when using Paperclip 使用回形针形式上传多张图片

Rails 4 | Unpermitted parameter: images when using Paperclip to upload multiple images using Paperclip form

在我的表单中使用简单表单从回形针上传多张图片时遇到强参数问题。我经常在服务器上弹出错误

"Unpermitted parameters: images"

当表单尝试上传到数据库时。好的,这就是我到目前为止所得到的。

表单代码:

<%= f.input :images, as: :file, input_html: { multiple: true } %>

办公桌控制器:

def new
    @desk = current_user.desks.build

    @desk.office_type_id = params[:office_type_id]
    @desk.desk_type_id = params[:desk_type_id]

    @amenities = Amenity.all
    @desk.amenity_ids = params[:amenities]

end

def create
    @desk = current_user.desks.build(desk_params)


    if @desk.save

        if params[:images]
            params[:images].each do |image|
                @desk.photos.create(image: image)
            end
        end

        @photos = @desk.photos
        redirect_to edit_desk_path(@desk), notice: "Saved..."
    else
        render "new"
    end
end


private
    def set_desk
        @desk = Desk.find(params[:id])
    end

def desk_params
    params.require(:desk).permit(:office_type_id, :desk_type_id, 
        :office_type, :desk_type, :listing_name, :min_days, :max_days, 
        :accommodate, :daily_rate, :location, :description, :amenities, 
        amenity_ids: [], 
        :photos_attributes => [ :images ])
end

我有照片模特:

class Photo < ActiveRecord::Base
    belongs_to :desk

    has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" }
    validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
end

办公桌型号:

    has_many :photos

我试过白名单 .., :images 和 .., :images => [] ,

均未成功。我确定我已经尝试过其他组合,这些组合将允许数组进入组合。

值得注意的一点是多张图片上传的服务器代码示例显示一个数组被拉入参数。示例:

Processing by DesksController#update as HTML


 Parameters: {"utf8"=>"✓", "authenticity_token"=>"hgEIE6yirx7ixY8Xs0mUxA+6G3wmy/D4wKSLKrF+clTfzvpunFG11PmEfI2l3bPV0IlCywdmvajshMph7c5V+A==", "desk"=>{"office_type_id"=>"1", "desk_type_id"=>"1", "accommodate"=>"1", "min_days"=>"1", "max_days"=>"2", "listing_name"=>"1111", "description"=>"111", "location"=>"111", "daily_rate"=>"100", "amenity_ids"=>["2", "3", ""], "images"=>[#<ActionDispatch::Http::UploadedFile:0x007fba71ce2880 @tempfile=#<Tempfile:/var/folders/gx/86yj74bx3md88cfn2fwc975h0000gn/T/RackMultipart20170713-20588-1bin85a.JPG>, @original_filename="IMG_1420.JPG", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"desk[images][]\"; filename=\"IMG_1420.JPG\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x007fba71ce2268 @tempfile=#<Tempfile:/var/folders/gx/86yj74bx3md88cfn2fwc975h0000gn/T/RackMultipart20170713-20588-1ac6bwu.JPG>, @original_filename="IMG_1421.JPG", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"desk[images][]\"; filename=\"IMG_1421.JPG\"\r\nContent-Type: image/jpeg\r\n">]}, "commit"=>"Update Desk", "id"=>"37"}

这让人非常沮丧。希望对这里发生的事情有任何帮助或指导。我确信这可能是一个嵌套问题,但仍然无法让它与当前代码一起使用。万分感谢

Desk Model 应该包括允许它创建照片对象的东西,例如:

has_many :photos
accepts_nested_attributes_for :photos

Desk Controller 不仅应该包括一组照片 ID 的可能性,还应该包括创建照片 ID 所需的特定属性,默认是一个 ID,然后是一个作为附件的图像:

def desk_params
    params.require(:desk).permit(:office_type_id, :desk_type_id, 
        :office_type, :desk_type, :listing_name, :min_days, :max_days, 
        :accommodate, :daily_rate, :location, :description, :amenities, 
        amenity_ids: [], photo_ids: [], photo_attributes: [ :id, :image ])
end

我会在 view/controller 逻辑中使用参数 param[:photo_ids] 而不是 desk 上的 images 以避免混淆并映射关系。

此外,建议您查看开发日志根据未经允许的参数吐出的内容,并查看浏览器控制台中的参数以确切了解尝试发布的内容。

在您的 rails 控制台中(因为我看不到您的模式,可能是输出):

Desk.column_names

Photo.column_names

确认您的参数并检查 model/schema 哪些是必需的且不能为空(如果 null => false 则没有默认值)

Unpermitted parameters: images

当您查看 params 哈希时,您没有 photos_attributes 密钥。 images: [] 直接进入办公桌。所以需要将desk_params修改为下面的

def desk_params
  params.require(:desk).permit(:office_type_id, :desk_type_id, 
  :office_type, :desk_type, :listing_name, :min_days, :max_days, 
  :accommodate, :daily_rate, :location, :description, :amenities, 
  amenity_ids: [], images: [])
end

好的,所以我最终在@irldexter 和@pavan 的帮助下弄清楚了这里的问题。他们提出了一些不太正确的建议,但最终确实帮助我解决了问题。我已经添加了一个答案以将它们放在一起。

所以当我查看 rails_form 和 simple_form 如何发送参数时,我注意到的问题是 header 中的 html 名称不同在每一个上。我还注意到,因此 simple_form 将我的桌面上的图像包含在内=>{} 而不是将其分离到自己的数组中。

Simple-form(不工作)

Parameters: {"utf8"=>"✓", 
"authenticity_token"=>"1c3dbJYJWuPs3L95F2eqkxm8Fuwr5J3D5LdEyRLeS7sAYgnbhEBIcfsE1A4Hw2rQy8zbZEdr6mMcZTjUGH3Cpg==", 
"desk"=>{"listing_name"=>"Dublin Desk Image", 
"description"=>"Desk in Dublin", 
"accommodate"=>"1", 
"price"=>"100", 

"images"=>#<ActionDispatch::Http::UploadedFile:0x007fce7cf618d0 @tempfile=#<Tempfile:/var/folders/gx/86yj74bx3md88cfn2fwc975h0000gn/T/RackMultipart20170718-1255-luxbhv.JPG>,
@original_filename="IMG_1421.JPG", 
@content_type="image/jpeg", 
@headers="Content-Disposition: form-data; name=\"desk[images]\"; 
filename=\"IMG_1421.JPG\"\r\nContent-Type: image/jpeg\r\n">},

 "commit"=>"Create Desk"

Rails 表格(工作)

    Parameters: {"utf8"=>"✓", 
"authenticity_token"=>"8YqUPPx9qumESGiSjkZCtxunFPsdeFed+pXmamWKwmY8jfwRXXCNDTUSJOdfT9R2b13NLOOGyqo3gOiP8FE08Q==", 

"office"=>{
"office_type"=>"Private", "desk_type"=>"Corporate", "accommodates"=>"2", "min_hours"=>"3", "max_hours"=>"2", "listing_name"=>"image test", "summary"=>"rgerg", "address"=>"grafton street, dublin 1", "is_aircon"=>"1", "is_elevator"=>"0", "is_showers"=>"0", "is_changing_room"=>"0", "is_printer"=>"1", "is_kitchen"=>"0", "is_telephone"=>"0", "is_desktop_computer"=>"0", "is_coffee_machine"=>"0", "is_meeting_room"=>"0", "is_wifi"=>"0", "price"=>"123", "active"=>"1"}, 

"images"=>[#<ActionDispatch::Http::UploadedFile:0x007fbeef60fa08 @tempfile=#<Tempfile:/var/folders/wj/p88yb82j39x40kgcm1qg24g00000gn/T/RackMultipart20170718-96629-11meflg.jpg>, @original_filename="benjamin-child-17946.jpg", 
@content_type="image/jpeg", 
@headers="Content-Disposition: form-data; name=\"images[]\"; 
filename=\"benjamin-child-17946.jpg\"\r\nContent-Type: image/jpeg\r\n">], 

"commit"=>"Save"}

当我重新创建问题时,简单的表单内容得到了简化。我需要在简单格式代码中以某种方式更改 name= 。当我意识到问题时,就这么简单。

<%= f.input :images, as: :file, input_html: { name: 'images[]' } %>

只要调用就可以的强参数

    def desk_params
        params.require(:desk).permit(:listing_name, :description, :accommodate, :price, :active, :images)
    end

我还注意到一定有一些问题没有找到参数,因为代码是 运行

if params[:images]
        params[:images].each do |image|
            @desk.photos.create(image: image)
        end
    end

当我在 put 消息中添加代码以查看它是否返回真值时。我还在 Desk 模型中添加了

accepts_nested_attributes_for :photos

尽管最后的修复很简单,但这只是因为提到了网络 headers 并且可能以简单的形式使用了 f.input_field,它删除了任何表单包装器以及名称:改变方法。感谢@pavan @irldexter 的帮助和建议。它帮助我了解有关发送的实际表单数据的更多信息。非常尊重。