问题
I'm trying to upload an image with paperclip gem, just now, my models are:
My model
class Advert < ActiveRecord::Base
has_attached_file :image
#accepts_nested_attributes_for :image
end
Part of my view:
<%= form_for :advert do |f| %>
<p>
<%= f.label :image %><br>
<%= f.file_field :image %>
</p>
<% end %>
My Controller:
def new
logger.info "Processing the request New..."
@advert = Advert.new
end
def create
logger.info "Processing the request Create..."
#logger.info JSON.parse( params[:advert].to_json )
@advert = Advert.new( advert_params )
@advert.save
redirect_to action: "index"
end
private
def advert_params
params.require(:advert).permit(:title, :features, :description, :areadescription, :rooms, :bathroom, :price, :type, :source, :originaldate, images_attributes: [:image])
end
And the request:
Processing by AdministrationController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"VGRNZCT8eo231iT2Fa2Bq2EsG+/4kfkzpgV7NQA4/6o=", "advert"=>{"title"=>"dfas", "features"=>"", "description"=>"", "areadescription"=>"", "rooms"=>"", "bathroom"=>"", "price"=>"", "type"=>"", "source"=>"", "originaldate"=>"", "image"=>#<ActionDispatch::Http::UploadedFile:0x007fb56fc34c78 @tempfile=#<Tempfile:/var/folders/sv/h26bfj7167jgnb1nm_nstw300000gn/T/RackMultipart20140417-540-iychs8>, @original_filename="i04.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"advert[image]\"; filename=\"i04.png\"\r\nContent-Type: image/png\r\n">}, "commit"=>"Save Advert"}
Processing the request Create...
Unpermitted parameters: image
I have to permit the image and add in the model, what changes I can do?
回答1:
Firstly you only need to include the attached file image
in your strong parameters:
def advert_params
params.require(:advert).permit(:title, :features, :description, :areadescription, :rooms, :bathroom, :price, :type, :source, :originaldate, :image)
end
The four attributes that Paperclip uses (image_file_name
, image_file_size
, image_content_type
and image_updated_at
) will be handled automatically when it saves to the database.
Secondly you need to add validation to the image in your model. Since Paperclip 4.0, at a minimum you must validate the content type of the image, or explicitly state you do not want to. So in you model you need to add one of the following:
If you want to validate the content type then something like:
validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/gif", "image/png"]
If you want to validate only the file name then something like:
validates_attachment_file_name :image, :matches => [/jpe?g\z/, /gif\z/, /png\z/]
If you want to explictly not validate the content of the attachment then:
do_not_validate_attachment_file_type :image
Or you can combine validation using validates_attachment
if you need to check that the image is present, of a specific type, and no greater than a certain file size for example. For more information, please see the documentation here: https://github.com/thoughtbot/paperclip
回答2:
remove the image_attributes and just add the :image to the permit hash
来源:https://stackoverflow.com/questions/23137495/ruby-on-rails-unpermitted-parameters-image