Can someone help me with the following code? I''m purposely typing in a
bogus rga number, and the begin/rescue block should catch that and
inform the user, right?
But when I run this with a bogus number, I get the "Render and/or
redirect were called multiple times in this action" error.
What am I doing wrong here?
Thanks!
def assign
@users = User.find(:all, :order => ''name'')
if request.post?
rga_number = params[:rga_number]
user_id = params[:user_id]
begin
rga = Rga.find(params[:rga_number])
rescue
flash[:notice] = ''There is not a RGA with that number''
redirect_to :action => ''list''
end
new_RGA = RgaEvent.new
new_RGA.rga_id = rga_number
new_RGA.rga_number = rga_number
new_RGA.user_id = user_id
if new_RGA.save
user_name = User.find(user_id).name
flash[:notice] = ''RGA assigned to user '' + user_name
redirect_to :action => ''list''
end
end
end
--
Posted via http://www.ruby-forum.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
-~----------~----~----~----~------~----~------~--~---
On 10/31/07, Jason Floyd <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Can someone help me with the following code? I''m purposely typing in a > bogus rga number, and the begin/rescue block should catch that and > inform the user, right?It does; that''s not the problem.> > But when I run this with a bogus number, I get the "Render and/or > redirect were called multiple times in this action" error. > > What am I doing wrong here? > > Thanks! > > def assign > @users = User.find(:all, :order => ''name'') > > if request.post? > rga_number = params[:rga_number] > user_id = params[:user_id] > > begin > rga = Rga.find(params[:rga_number]) > rescue > flash[:notice] = ''There is not a RGA with that number'' > redirect_to :action => ''list''You need to "return" right here, or execution continues on. That''s why you''re getting the error message. You probably should rescue ActiveRecord::RecordNotFound. If a different exception were to be raised, you would never know it.> end > > new_RGA = RgaEvent.new > new_RGA.rga_id = rga_number > new_RGA.rga_number = rga_number > new_RGA.user_id = user_id > > if new_RGA.save > user_name = User.find(user_id).name > flash[:notice] = ''RGA assigned to user '' + user_name > redirect_to :action => ''list'' > end > > end > 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 -~----------~----~----~----~------~----~------~--~---
Ok,
I''ve changed the code to:
begin
rga = Rga.find(params[:rga_number])
rescue ActiveRecord::RecordNotFound
flash[:notice] = ''There is not a RGA with that number''
redirect_to :action => ''list''
end
Still getting the error. You''re dealing with a newbie here: What do
you mean by ''return''?
--
Posted via http://www.ruby-forum.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
-~----------~----~----~----~------~----~------~--~---
On 10/31/07, Jason Floyd <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Ok, > > I''ve changed the code to: > > begin > rga = Rga.find(params[:rga_number]) > rescue ActiveRecord::RecordNotFound > flash[:notice] = ''There is not a RGA with that number'' > redirect_to :action => ''list'' > end > > Still getting the error. You''re dealing with a newbie here: What do > you mean by ''return''?I mean to add a "return" statement: redirect_to :action => ''list'' return # <-- add this line 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 -~----------~----~----~----~------~----~------~--~---
> I mean to add a "return" statement: > > redirect_to :action => ''list'' > return # <-- add this line > endWorked perfectly! Thanks! I''ve used the begin/rescue/end in other controllers like I did in this one, so I''m still confused as to why this case required a return in it. -- Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---
On 31 Oct 2007, at 14:07, Jason Floyd wrote:> >> I mean to add a "return" statement: >> >> redirect_to :action => ''list'' >> return # <-- add this line >> end > > Worked perfectly! Thanks! > > I''ve used the begin/rescue/end in other controllers like I did in this > one, so I''m still confused as to why this case required a return in > it.This has nothing to do with begin/rescue. It has everything to do with the fact that you should redirect or render precisely once per action, whereas in you original code you would end up doing a redirect in the rescue block and then again if bit below. Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> you should redirect or render precisely once per action,How do you possibly check for multiple errors in an action? Aren''t you supposed to redirect after detecting one? -- Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---
On 31 Oct 2007, at 14:24, Jason Floyd wrote:> > >> you should redirect or render precisely once per action, > > How do you possibly check for multiple errors in an action? Aren''t > you > supposed to redirect after detecting one?OK, so at a basic level, this will result in the double render error def foo redirect_to :action => ''bar'' redirect_to :action => ''bar'' end so def foo if ... redirect_to :action => ''bar'' end if ... redirect_to :action => ''bar'' end end will also cause errors unless the 2 conditions are mutually exclusive You need to do something like. Either return after redirecting/ rendering or do whatever you have to do to ensure that you don''t do it more than once per action, for example def foo if ... redirect_to :action => ''bar'' return end if ... redirect_to :action => ''bar'' return end end Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ok, that makes sense. Thanks guys! -- Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---