Ruby on rails 不允许的参数:图像 [英] Ruby on rails Unpermitted parameters: image

查看:35
本文介绍了Ruby on rails 不允许的参数:图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试上传带有回形针宝石的图像,刚才,我的模型是:

I'm trying to upload an image with paperclip gem, just now, my models are:

我的模型

class Advert < ActiveRecord::Base
    has_attached_file :image
    #accepts_nested_attributes_for :image
end

我的部分观点:

<%= form_for :advert  do |f| %>
  <p>
    <%= f.label :image %><br>
    <%= f.file_field :image %>
  </p>
<% end %>

我的控制器:

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

和请求:

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?

推荐答案

首先你只需要在你的强参数中包含附件image:

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

Paperclip 使用的四个属性(image_file_nameimage_file_sizeimage_content_typeimage_updated_at)将被自动处理当它保存到数据库时.

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.

其次,您需要为模型中的图像添加验证.从 Paperclip 4.0 开始,您至少必须验证图像的内容类型,或者明确声明您不想验证.因此,在您的模型中,您需要添加以下内容之一:

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

或者您可以使用 validates_attachment 组合验证,例如,如果您需要检查图像是否存在,是否属于特定类型,并且不大于特定文件大小.有关更多信息,请参阅此处的文档:https://github.com/thoughtbot/paperclip

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

这篇关于Ruby on rails 不允许的参数:图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆