Forum: Ruby on Rails how to show multiple errors

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ffb15a8418eb7f49060b6764aba1dc52?d=identicon&s=25 Rm Rm (rmehta)
on 2007-01-29 07:25
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.
C4dc94c893471878a105761a9207f29b?d=identicon&s=25 Zack Chandler (Guest)
on 2007-01-29 07:44
(Received via mailing list)
On 1/28/07, Rm Rm <rails-mailing-list@andreas-s.net> 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 Chandler
http://depixelate.com
Ffb15a8418eb7f49060b6764aba1dc52?d=identicon&s=25 Rm Rm (rmehta)
on 2007-01-29 08:04
no, these error are not from models but are from actions.



Zack Chandler wrote:
> On 1/28/07, Rm Rm <rails-mailing-list@andreas-s.net> 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 Chandler
> http://depixelate.com
C4dc94c893471878a105761a9207f29b?d=identicon&s=25 Zack Chandler (Guest)
on 2007-01-29 08:12
(Received via mailing list)
On 1/28/07, Rm Rm <rails-mailing-list@andreas-s.net> 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 Chandler
http://depixelate.com
Ffb15a8418eb7f49060b6764aba1dc52?d=identicon&s=25 Rm Rm (rmehta)
on 2007-01-29 08:50
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 Chandler wrote:
> On 1/28/07, Rm Rm <rails-mailing-list@andreas-s.net> 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 Chandler
> http://depixelate.com
70225136eacd3d870f64e03bff678655?d=identicon&s=25 Russell Norris (Guest)
on 2007-01-29 14:54
(Received via mailing list)
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
Ffb15a8418eb7f49060b6764aba1dc52?d=identicon&s=25 Rm Rm (rmehta)
on 2007-02-02 02:58
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 %>
&nbsp;
<% 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 Norris 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
C4dc94c893471878a105761a9207f29b?d=identicon&s=25 Zack Chandler (Guest)
on 2007-02-02 06:06
(Received via mailing list)
On 2/1/07, Rm Rm <rails-mailing-list@andreas-s.net> 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 %> </br>
  Password: <%= password_field :user, :password %> </br>
  Password: <%= password_field :user, :password_confirmation %> </br>
  Email: <%= text_field :user, :email_address %> </br>
  <%= 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 Chandler
http://depixelate.com
This topic is locked and can not be replied to.