Richlewis
Richlewis

Reputation: 15374

Carrierwave error messages not being displayed

I have a form that uploads files via an ajax call (using the remotipart gem), not sure if this is related but when validation fails with file type (using carrierwave white list), I can't get the error message to display in the view, even though other error messages will show (say when file size is to large).

So in my Uploader class

class MediaUploader < CarrierWave::Uploader::Base
  def extension_white_list
    %w(jpg jpeg gif png docx mp4 pdf)
  end
end

So in my form I will attatch a csv file, click submit and the request and responses look like so

Processing by DocumentsController#create as JS
Parameters: {"utf8"=>"✓", 
             "document"=>
                {"skill_id"=>"1",
                 "media_cache"=>"",
                 "media"=>#<ActionDispatch::Http::UploadedFile:0x000000070157a0 @tempfile=#<Tempfile:/tmp/RackMultipart20150205-4409-ltyy7t.csv>,              @original_filename="csvtest.csv", @content_type="text/csv", @headers="Content-Disposition: form-data; name=\"document[media]\"; filename=\"csvtest.csv\"\r\nContent-Type: text/csv\r\n">}, 
                 "commit"=>"Upload", 
                 "remotipart_submitted"=>"true",
                 "authenticity_token"=>"token here", 
                 "X-Requested-With"=>"IFrame", 
                 "X-Http-Accept"=>"text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01"
             }

 (1.8ms)  BEGIN
 (2.7ms)  ROLLBACK

In my controller I have grabbed the error messages @document.errors

#<ActiveModel::Errors:0x0000000666e418 
@base=#
  <Document id: nil, 
            media: nil, 
            user_id: 1, 
            skill_id: 1, 
            created_at: nil, 
            updated_at: nil>, 

            @messages={:media=>[
                                "You are not allowed to upload \"csv\" files, allowed types: jpg, jpeg, gif, png, docx, mp4, pdf", 
                                "At least 1 File is required"
                               ]
                      }>

After this the relevant controller action/views are called

Rendered documents/_document_form.html.erb (5.1ms)
Rendered documents/create.js.erb (7.7ms)
Completed 200 OK in 167ms (Views: 83.2ms | ActiveRecord: 64.6ms)

My form is set out to iterate through each of the error messages but it doesn't display them

_document_form

 <%= form_for @document, remote: true, html: { multipart: true, id: @document.object_id.to_s, class: 'upload_document' } do |f| %>
  <% if @document.errors.any? %>
    <h2><%= pluralize(@document.errors.count, "error") %> prohibited this record from being saved</h2>
      <ul class="error_list">
        <% @document.errors.full_messages.each do |msg| %>
          <li><%= error_edit(msg) %></li>
        <% end %>
      </ul>
  <% end %>

  <%= f.hidden_field :skill_id %>

  <%= f.label :media %>

  <%= f.file_field :media %> 

  <%= f.submit 'Upload', class: 'btn btn-success' %>
  <div class="js-error"></div>

<% end %>

create action

def create
@document = current_user.documents.new(document_params)
respond_to do |format|

   if @document.save
     format.html { redirect_to root_path, notice: success_save_msg }
     #format.js { render :js => "window.location.href='"+root_path+"'", notice: success_save_msg }
   else
     format.html
     format.js { render action: 'create.js.erb' }
     ap(@document.errors)
   end

end
end

create.js.erb

$("#<%= @document.skill_id %>").html("<%= j render(partial: 'documents/document_form', locals: { skill_id: @document.skill_id }) %>")

Also I'm looking at the response in the net tab and it's clearly returning the error messages:

<textarea data-type="text/javascript" data-status="200" data-statusText="OK">$
("#1").html("<\n<form id=\"55100200\" class=\"upload_document\" enctype=\"multipart/form-data\" action=\"/documents\" accept-charset=\"UTF-8\" data-remote=\"true\" method=\"post\"><input name=\"utf8\" type=\"hidden\" value=\"&#x2713;\" />\n    <div id=\"error_explanation\" class=\"alert alert-dismissable\">\n      <button type=\"button\" class=\"close\" data-dismiss=\"alert\"><span aria-hidden=\"true\">&times;<\/span><span class=\"sr-only\">Close<\/span><\/button>\n      <h2>2 errors prohibited this record from being saved<\/h2>\n        <ul class=\"error_list\">\n            <li>  You are not allowed to upload &quot;csv&quot; files, allowed types: jpg, jpeg, gif, png, docx, mp4, pdf<\/li>\n            <li>  At least 1 File is required<\/li>\n        <\/ul>\n    <\/div>\n\n  <input value=\"1\" class=\"skill_id\" type=\"hidden\" name=\"document[skill_id]\" id=\"document_skill_id\" />\n\n  <label class=\"custom-file-upload btn btn-info\" for=\"document_media_60835260\">\n    <span class=\"glyphicon glyphicon-cloud-upload\"><\/span>\n     Find File\n<\/label>\n  <input class=\"document_file_field\" id=\"document_media_60835260\" type=\"file\" name=\"document[media]\" />\n  <input type=\"hidden\" name=\"document[media_cache]\" id=\"document_media_cache\" />\n\n  <input type=\"submit\" name=\"commit\" value=\"Upload\" class=\"btn btn-success\" />\n  <div class=\"js-error\"><\/div>\n\n<\/form>")
</textarea>

Can anyone see why the error messages would not display when coming from carrierwave as opposed to a validation from within my Document model?

Upvotes: 1

Views: 1978

Answers (2)

ltdev
ltdev

Reputation: 4467

Thats my en.yml

   en:
      hello: "Hello world"
      errors:
        messages:
          extension_white_list_error: 'My Custom Message'

enter image description here

EDIT This fixed my script

en:
  # hello: "Hello world"
  errors:
    messages:
      extension_whitelist_error: "You are not allowed to upload %{extension} files, allowed types: %{allowed_types}"

Upvotes: 0

Richlewis
Richlewis

Reputation: 15374

Thought I Would share this so if anyone else has the same problem it might help. I came across this this Blog. Which then led me to This stackoverflow post.

Turns out i just needed to set the message (or custom message if I wanted) in my en.yml file and carrierwave did the rest

en:
  errors:
    messages:
      extension_white_list_error: 'My Custom Message'

Hope this helps someone

Upvotes: 4

Related Questions