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