How to show multiple errors

Hi,

In my form I have about 10 fields and after validating them, I need to
show all the validation errors. But

<% if @flash[:error] %>
<%= @flash[:error] %>
<% end %>

is showing only the last error being put in the flash.

How can I show multiple errors using flash or otherwise ?

Thanks.

On 1/28/07, Rm Rm [email protected] wrote:

is showing only the last error being put in the flash.

How can I show multiple errors using flash or otherwise ?

Thanks.


Posted via http://www.ruby-forum.com/.

If these are errors from your models you can use error_messages_for(…)

<%= error_messages_for :user %>

Hope this helps.


Zack C.
http://depixelate.com

no, these error are not from models but are from actions.

Zack C. wrote:

On 1/28/07, Rm Rm [email protected] wrote:

is showing only the last error being put in the flash.

How can I show multiple errors using flash or otherwise ?

Thanks.


Posted via http://www.ruby-forum.com/.

If these are errors from your models you can use error_messages_for(…)

<%= error_messages_for :user %>

Hope this helps.


Zack C.
http://depixelate.com

On 1/28/07, Rm Rm [email protected] wrote:

<%= error_messages_for :user %>

I can’t think of an example of when you’d have multiple errors when
not dealing with models. Are you sure you’re not talking about errors
in saving, creating, or modifying ActiveRecord models? Give an
example of 10 errors.


Zack C.
http://depixelate.com

I am still stuck on this problem. I will explain what I am trying to do:

On my form I have fields to creae a new user. For that I have a login
id, pwd1 and pwd2 and email fields alongwith other fields. Now if login
already exists, or two passwords don’t match or email is not in a corret
in proper format, then I need to show all the error alongwith the values
entered by the user. I am able to show only the last error put in
flash[:error] as:

flash[:error] = msg

All the error that I have put in before are not displayed in the view by
:
<% if flash[:error] -%>
<%= flash[:error]%>
<% flash[:error] = nil %>
<%else %>
 
<% end -%>

Also how can I fill the form with the values entered by the user when
view is displayed again ? I am new to ruby and rails, hence this seems
like a big task for me. Any help is really appreciated.

Russell N. wrote:

I think the problem is that you’re assigning multiple errors to
flash[:error] like this:

flash[:error] = “whatever”

and that will clobber all your old assignments. If this is the case, you
can
just do this at the beginning of your controller method:

flash[:error] = []

and then push error messages into the Array. You could use a hash if you
wanted that functionality. There’s a couple of different ways to do that
but
the important thing is to remember that while flash is a hash,
flash[:whatever] isn’t [unless you make it so].

Then again, this might not be the problem but since you didn’t post any
code
I’m having to assume it is.

RSL

I think the problem is that you’re assigning multiple errors to
flash[:error] like this:

flash[:error] = “whatever”

and that will clobber all your old assignments. If this is the case, you
can
just do this at the beginning of your controller method:

flash[:error] = []

and then push error messages into the Array. You could use a hash if you
wanted that functionality. There’s a couple of different ways to do that
but
the important thing is to remember that while flash is a hash,
flash[:whatever] isn’t [unless you make it so].

Then again, this might not be the problem but since you didn’t post any
code
I’m having to assume it is.

RSL

Fields on the form will be added to different tables in database after
validation.
Like used details will go to user table, address will go to address
table and so on. I am new to RoR , hence may be I am not able to explain
excatly in terms of models and views.

Thanks for the help.

Zack C. wrote:

On 1/28/07, Rm Rm [email protected] wrote:

<%= error_messages_for :user %>

I can’t think of an example of when you’d have multiple errors when
not dealing with models. Are you sure you’re not talking about errors
in saving, creating, or modifying ActiveRecord models? Give an
example of 10 errors.


Zack C.
http://depixelate.com

On 2/1/07, Rm Rm [email protected] wrote:

I am still stuck on this problem. I will explain what I am trying to do:

On my form I have fields to creae a new user. For that I have a login
id, pwd1 and pwd2 and email fields alongwith other fields. Now if login
already exists, or two passwords don’t match or email is not in a corret
in proper format, then I need to show all the error alongwith the values
entered by the user. I am able to show only the last error put in
flash[:error] as:

It sounds like you’d benefit from reviewing the basics of creating
models (a new user in this case) from forms. I highly recommend the
AWDWR book for this. But to get you jumpstarted you can use the
following code as an example:

<%= error_messages_for :user %>

<% form_tag :action => ‘create’ do %>
Username: <%= text_field :user, :name %>

Password: <%= password_field :user, :password %>

Password: <%= password_field :user, :password_confirmation %>

Email: <%= text_field :user, :email_address %>

<%= submit_tag ‘Create user’ %>
<% end %>

<% end -%>

Also how can I fill the form with the values entered by the user when
view is displayed again ? I am new to ruby and rails, hence this seems
like a big task for me. Any help is really appreciated.

In your controller’s action:

def create
@user = User.create(params[:user])
if @user.valid?
flash[:success] = ‘User created.’
redirect_to :action => ‘index’
else
render :action => ‘new’
end
end

This will preserves the form values.

Hope this helps.


Zack C.
http://depixelate.com