Note that there are some explanatory texts on larger screens.

plurals
  1. PORails paperclip content type validation with client_side_validations
    primarykey
    data
    text
    <p>I am using paperclip for images. I want to make sure that only images files(jpeg/png) are uploaded. Here is my paperclip validation rule.</p> <pre><code>validates_attachment :photo, :presence =&gt; { :message =&gt; "is required" }, :content_type =&gt; { :content_type =&gt; ['image/jpeg', 'image/jpg', 'image/png'], :message =&gt; "Invalid content type" } </code></pre> <p>The validation works and record is not saved if user uploads a non-image file. But the error message gets stored in @news.errors.messages[:<strong>photo_content_type</strong>] instead of @news.errors.messages[:<strong>photo</strong>]</p> <p>I am using client-side-validations gem to display error messages. Since invalid content type message is attached to :<strong>photo_content_type</strong>, it is not displayed inline.</p> <p>Right now I am displaying the message as follows:</p> <pre><code>&lt;% if @news.errors.messages[:photo_content_type] %&gt; &lt;div class="field_with_errors"&gt; &lt;%= f.file_field :photo %&gt; &lt;label for="" class="validation-error-message"&gt; &lt;%= @news.errors.messages[:photo_content_type][0] %&gt; &lt;/label&gt; &lt;/div&gt; &lt;% else %&gt; &lt;%= f.file_field :photo %&gt; &lt;% end %&gt; </code></pre> <p>Is there a better way so that If user uploads a non image file, the message gets added to @news.errors.messages[:photo] </p> <p><strong>Update</strong><br> I added after validation callback:</p> <pre><code>after_validation :join_img_errors def join_img_errors if errors.messages.has_key?(:photo_content_type) errors.messages[:photo] = errors.messages[:photo_content_type] end end </code></pre> <p>This way, I don't have to modify view code. But I still have to add the callback to all the models that use paperclip.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload