Search code examples
ruby-on-railspaperclip-validation

Paperclip validation present: true not working


I am using Paperclip with S3 in my Rails app. This is my model:

  has_attached_file :cv,
                      storage: :s3,
                      s3_host_name: 's3-eu-west-1.amazonaws.com',
                    s3_credentials: {access_key_id: ENV["AWS_KEY"], 
                                                     secret_access_key: ENV["AWS_SECRET"], 
                                                     bucket: ENV["AWS_BUCKET"]
                                                     }

    validates_attachment_presence :cv, message: "No file."
    validates_attachment_file_name :cv, matches: [/pdf\Z/, /docx\Z/, /doc\Z/, /txt\Z/], message: "Formats just in .pdf, .doc a docx."
    validates_with AttachmentSizeValidator, attributes: :cv, less_than: 3.megabytes, message: "Too large."

This is my controller

 def create
    @doc = Document.new(doc_params)
    if @doc.save
      redirect_to users_new_path(token: @doc.token)
    else
      flash[:alert] =  @doc.errors.messages.first.second.first
      redirect_to documents_new_path
     end
  end

  def doc_params
    params.require(:document).permit(:cv)
  end

In params it is just the file, nothing else. Everything is working - S3, validation of file_name, size... the only thing that is not working is the validation of present.

If no file added, I am getting an error:

param is missing or the value is empty: document

I am expecting to be redirecting instead and show the custom error message. I tried the other commands for validation of presennce from the PaperClip documentation but no luck. What is wrong?

EDIT: Form code:

<%= form_for @doc do |f| %>
  <div class="input-group input-group-lg" id="fileupload">
    <span class="input-group-btn">
      <span class="btn btn-default btn-file" id="gray-button">
        <i class="glyphicon glyphicon-file"></i> 
        <%= f.file_field :cv, id: "fileopen", type: "file" %>
      </span>
    </span>
    <input type="text" class="form-control text-center" id="filepath" placeholder="Find your file..." readonly>
  </div>
  <br>
  <%= f.button :cv, type: "submit", id: "upload-button", class: "btn-lg btn-primary" do %>
    UPLOAD
  <%end%>
<%end%>

Solution

  • Replace your strong parameters with:-

    def doc_params
        params.fetch(:document, {}).permit(:cv)
    end
    

    When a file is not uploaded your params will not contain the document key, while your strong params 'requires' it.