【发布时间】:2017-07-13 15:19:53
【问题描述】:
在我的表单中使用简单表单从回形针上传多个图像时,遇到了强大的参数。我经常在服务器上弹出错误
“不允许的参数:图像”
当表单尝试上传到数据库时。好的,这就是我到目前为止所得到的。
表格代码:
<%= 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"}
这非常令人沮丧。希望对这里发生的事情有任何帮助或指导。我确信这可能是一个嵌套问题,但仍然无法让它与当前代码一起使用。万分感谢。
【问题讨论】:
标签: ruby-on-rails-4 paperclip simple-form strong-parameters