[英]unpermitted parameters when saving paperclip image
我在參數方面遇到了一些麻煩,所以我想,我已經看到了Ruby on rails不允許的參數:image並且沒有用
我在我的pictures_controller.rb中
def create
@picture = Picture.new(picture_params)
if @picture.save
#ok
else
#error
end
end
private
def picture_params
params.require(:picture).permit(:project_id, :picture )
end
在我的世界里
Parameters: {"utf8"=>"✓", "picture"=>{"project_id"=>"10", "picture"=>[#<ActionDispatch::Http::UploadedFile:0x007f9e9ccd1378 @tempfile=#<Tempfile:/tmp/RackMultipart20140604-18563-1ce54ta>, @original_filename="apple-touch-icon.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"picture[picture][]\"; filename=\"apple-touch-icon.png\"\r\nContent-Type: image/png\r\n">]}}
Unpermitted parameters: picture
參數project_id已保存,但回形針的字段(文件名,文件大小和文件類型)未保存。
IN模型
has_attached_file :picture, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :picture, :content_type => ["image/jpg", "image/jpeg", "image/gif", "image/png"]
而且Picture不是嵌套屬性,project_id只是我在文本字段中獲得的值,因此根本沒有嵌套模型,它是一種簡單的形式,具有兩個字段project_id和一個名為picture的文件字段。
我解決了它,似乎在我的params中,帶有圖像數據的關鍵圖片是一個數組
"picture"=>[#<ActionDispatch::Http::UploadedFile:0x007f9e9ccd1378 @tempfile=#<Tempfile:/tmp/RackMultipart20140604-18563-1ce54ta>, @original_filename="apple-touch-icon.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"picture[picture][]\"; filename=\"apple-touch-icon.png\"\r\nContent-Type: image/png\r\n">]}
所以我只是做一個.first來獲取價值,我認為這不是最好的方法,但是我會找到一種更清潔的方法,至少現在我知道錯誤的出在哪里。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.