[Rails] Re: How do you use :message with validation?

Daniel N
Thu, 20 Jul 2006 10:50:56 -0000

On 7/20/06, Ben Lisbakken <lisbakke@gmail.com>
wrote:
>
> Jean-etienne Durand wrote:
> > <%= error_messages_for(''my_model'') %> in your
view.
> >
> > Some css style examples:
> > 154   /* error
> > 155      ---- */
> > 156   .fieldWithErrors {
> > 157     padding: 1px;
> > 158     background-color: red;
> > 159     display: table;
> > 160   }
> > 161   #errorExplanation {
> > 162     border: 2px solid red;
> > 163     padding: 7px;
> > 164     padding-bottom: 12px;
> > 165     margin-bottom: 10px;
> > 166     background-color: #f0f0f0;
> > 167   }
> > 168   #errorExplanation h2 {
> > 169     text-align: left;
> > 170     font-weight: bold;
> > 171     padding: 5px 5px 5px 15px;
> > 172     margin: -7px;
> > 173     background-color: #c00;
> > 174     color: #fff;
> > 175   }
> > 176   #errorExplanation p {
> > 177     color: #333;
> > 178     margin-bottom: 0;
> > 179     padding: 5px;
> > 180   }
> > 181   #errorExplanation ul li {
> > 182     list-style: square;
> > 183   }
>
> thanks - I''m afraid I wasn''t specific enough.  Wanted the
error message
> in my controller, not the view.
>
> here''s how I did it
>
>       @messages = @user.errors.full_messages
>       flash[:notice] = @messages[0]
>       for i in (1..@messages.length-1)
> flash[:notice] += " " + @messages[i]
>       end
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails


You can use the errors.on(:field) if you want to get the  message for a
field.  Or even just errors[:field]

As for you example code is there a reason why you couldn''t just use
something like

flash[:notice] = @user.errors.full_messages.join(" ")

The error_messages_for will provide you with a display in your view of all
these messages, and there is a post from Jean-etienne Durand with an example
style.

If you don''t want to display some of the portions of what is returned
from
error_messages_for why not control it with CSS.  eg

errorExplaination h2 { display:none; }
errorExplaination p { display:none; }

Just my 2c
-------------- next part --------------
An HTML attachment was scrubbed...