Changing Error Message in Rails Model

294 Views Asked by At

I have a Rails (3.1) app with a model validation that, when triggered, puts the model and field name before the message, e.g.:

Profile image profile image content type Only jpeg, gif and png files are allowed for profile pictures

Is there a way to avoid that, so it reads:

Only jpeg, gif and png files are allowed for profile pictures

model.rb validation:
validates_attachment_content_type :profile_image,
  :content_type => ['image/jpeg', 'image/png', 'image/gif'],
  :message      => "Only jpeg, gif and png files are allowed for profile pictures"

The error appears as a part of this code in my layout:

<% if object.errors.any? %>
  <div class="alert alert-message error" data-alert="alert">
    <a class="close" data-dismiss="alert">×</a>
    <ul>
      <% object.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
    </ul>
  </div>
<% end %>
1

There are 1 best solutions below

3
On

My hunch is that msg is not actually the message but the entire error hash, and so calling <%= msg %> actually converts the whole hash to a string, including the keys. You could confirm this with <%= msg.class %>.

Assuming the view code you posted is a partial it would be helpful to see the view that includes the partial. If it's not a partial it would be useful to see the surrounding code.