fredmsun
2008-Dec-31 23:23 UTC
How do you pass a Validation error to a view using redirect_to ?
Hi All
I have a problem with passing Validation error messages from my model
''Visitor'' to my view ''index.html.erb''.
When I pass an invalid surname from the form on my view, I correctly
get an invalid response on my error page when I use the render :action
=> ''error command in my controller.
However I want this error message to appear on my index page and I
want the form to be redirected to this page.
My question is how can I pass the error failure from my model to the
view, using a redirect_to?
Many thanks, and Happy New Year!
Ben...
index_controller.rb
============
def create
@visitor = Visitor.new(params[:visitor])
respond_to do |format|
if @visitor.save
format.html { redirect_to :action => ''index'' }
else
# format.html { render :action => ''error'' }
# format.html { render :controller => ''index'',
:action =>
''index'' }
format.html { redirect_to :action => ''index'' }
end
end
end
index.html.erb
========= <p class="form_failure clear"><%=
error_messages_for ''visitor''
%></p>
visitor.rb
=====
class Visitor < ActiveRecord::Base
validates_presence_of :surname, :message => ''is invalid!''
end
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Michael Libby
2009-Jan-01 11:26 UTC
Re: How do you pass a Validation error to a view using redirect_to ?
On Wed, Dec 31, 2008 at 5:23 PM, fredmsun <fredmsun-97jfqw80gc6171pxa8y+qA@public.gmane.org> wrote:> My question is how can I pass the error failure from my model to the > view, using a redirect_to?Redirect wipes out things like instance variables because you are making the browser ask for a new page. The flash is there to pass stuff from one action to the next. You can either pass the error messages... or even your whole invalid object along. Example usage in: ri ActionController::Flash -Michael -- Michael C. Libby www.mikelibby.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
fredmsun
2009-Jan-01 20:22 UTC
Re: How do you pass a Validation error to a view using redirect_to ?
Thanks Mike I''ve finally figured out the problem. The reason I didn''t use render was that when I did my page kept forwarding onto an unwanted URL route, so I used redirect_to, but I didn''t really think about the fact that I would loose the request data as redirect_to is a new request. Anyway it turns out that because I was using index_controller.rb and a view called index.html.erb that some how this was affecting my routing. I then switched to using a different RESTful resource and used render and it all worked like clockwork. Thanks for the pointers, Ben... On Jan 1, 11:26 am, "Michael Libby" <michael.c.li...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Wed, Dec 31, 2008 at 5:23 PM, fredmsun <fredm...-97jfqw80gc6171pxa8y+qA@public.gmane.org> wrote: > > My question is how can I pass the error failure from my model to the > > view, using a redirect_to? > > Redirect wipes out things like instance variables because you are > making the browser ask for a new page. > > The flash is there to pass stuff from one action to the next. You can > either pass the error messages... or even your whole invalid object > along. > > Example usage in: > > ri ActionController::Flash > > -Michael > > -- > Michael C. Libbywww.mikelibby.com--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---