validates_presence_of + :message shows the name of the field

model, ruby-on-rails, validation

Solution

The way that I do this is to output the message without the field name. For example, I have a partial that outputs the error messages after validation fails.

<ul>
    <% errors.each do |attribute, message| -%>
        <% if message.is_a?(String)%>
            <li><%= message %></li>
        <% end %>
    <% end -%>
</ul>

Notice that this does not output the attribute. You just need to make sure that all your messages makes sense without an attribute name.

Problem

I'm creating a Rails app, and I have a model called `User`. In this model I have a boolean value called `isagirl`. A user must specify if it is a girl or not, which is done by two radio buttons. In my model I have this: ``` validates_presence_of :isagirl, :message => "You must be either a Boy or a Girl. If not, please contact us." ``` However, when I don't specify a sex, I'm seeing this: Isagirl You must be either a Boy or a Girl. as an error message. The problem is that 'Isagirl' must not be there in the error message. How can I disable that? And no, using CSS to hide it is no option. Thanks

Original source