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.
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.
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.
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.
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.
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:
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