Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

ruby - Paperclip::Errors::MissingRequiredValidatorError with Rails 4

I'm getting this error when I try to upload using paperclip with my rails blogging app. Not sure what it is referring to when it says "MissingRequiredValidatorError" I thought that by updating post_params and giving it :image it would be fine, as both create and update use post_params

Paperclip::Errors::MissingRequiredValidatorError in PostsController#create
Paperclip::Errors::MissingRequiredValidatorError

Extracted source (around line #30):

def create
  @post = Post.new(post_params)

This is my posts_controller.rb

def update
  @post = Post.find(params[:id])

  if @post.update(post_params)
    redirect_to action: :show, id: @post.id
  else
    render 'edit'
  end
end

def new
  @post = Post.new
end

def create
  @post = Post.new(post_params)

  if @post.save
    redirect_to action: :show, id: @post.id
  else
    render 'new'
  end
end
#...

private

def post_params
  params.require(:post).permit(:title, :text, :image)
end    

and this is my posts helper

module PostsHelper
  def post_params
    params.require(:post).permit(:title, :body, :tag_list, :image)
  end
end

Please let me know if I can supplement extra material to help you help me.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Starting with Paperclip version 4.0, all attachments are required to include a content_type validation, a file_name validation, or to explicitly state that they're not going to have either.

Paperclip raises Paperclip::Errors::MissingRequiredValidatorError error if you do not do any of this.

In your case, you can add any of the following line to your Post model, after specifying has_attached_file :image

Option 1: Validate content type

validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]

-OR- another way

validates_attachment :image, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] }

-OR- yet another way

is to use regex for validating content type.

For example: To validate all image formats, regex expression can be specified as shown in

@LucasCaton's answer

Option 2: Validate filename

validates_attachment_file_name :image, :matches => [/png/, /jpe?g/, /gif/]

Option 3: Do not validate

If for some crazy reason (can be valid but I cannot think of one right now), you do not wish to add any content_type validation and allow people to spoof Content-Types and receive data you weren't expecting onto your server then add the following:

do_not_validate_attachment_file_type :image

Note:

Specify the MIME types as per your requirement within content_type/ matches options above. I have just given a few image MIME types for you to start with.

Reference:

Refer to Paperclip: Security Validations, if you still need to verify. :)

You might also have to deal with the spoofing validation explained here https://stackoverflow.com/a/23846121


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...