Trip
Trip

Reputation: 27114

how do I place custom error messages at the top of a form?

Such a novice question, but I can't seem to resolve this.

if I have a simple Rails 2.3.5 form :

- form_for CardSignup.new do |f|
   = f.error_messages
  .grid_1.alpha.align_right
    = f.label :zip_code, 'zip'
  .grid_1.omega.alpha
    = f.text_field :zip_code, :style => "width: 75px;"
    %div{:class => 'error_message'}
  .clear

How do I make the error_message appear at the top of the form, rather than in the particular place of the form?

I was using this : = f.error_messages , but it doesn't show any content when put at the top of the form.

Upvotes: 0

Views: 410

Answers (2)

rubyprince
rubyprince

Reputation: 17793

Why are you using CardSignup.new in the view rather than defining @card_signup = CardSignup.new in the controller? If you are doing like that you can call

<%= @card_signup.error_messages %>

or

<%= error_messages_for :card_signup %>

anywhere in the view file and it will show the error messages for @card_signup object and you can call your form like this:

<% form_for @card_signup do |f| %>

Upvotes: 0

neelabh
neelabh

Reputation: 577

hah I was looking for this too. Anyways this is how I figured it out. So all we need to do is update flash[:alert] or whatever and have that flash[:alert] at the top of your layout file. Now all u do if

<%= simple_form_for(@boing) do |f| %>
  <% if @boing.errors.any? %>
    <% flash[:alert] = "stuff" %>
  <% end %>
....

now you don't want <%= flash[:alert] = "stuff" %> otherwise it will display at both places-inline and at top as <%= %> is for displaying in browser. Hope that helps

Upvotes: 1

Related Questions