Hey guys, How do you stop Ruby code from executing? For example, def newaction puts "1" return puts "2" end I tried return in the following example, but it continues and than looks for newaction.rhtml. How do I stop it completely dead in its tracks? -- Posted via http://www.ruby-forum.com/.
Hey guys, How do you stop Ruby code from executing? For example, def newaction puts "1" return puts "2" end I tried return in the example above, but it continues and than looks for newaction.rhtml. How do I stop it completely dead in its tracks? -- Posted via http://www.ruby-forum.com/.
Alex Wayne
2006-Jul-27 22:18 UTC
[Rails] Re: How do you stop a Ruby method from executing?
Eric Gross wrote:> Hey guys, > > How do you stop Ruby code from executing? For example, > > def newaction > puts "1" > return > puts "2" > end > > > I tried return in the example above, but it continues and than looks for > newaction.rhtml. How do I stop it completely dead in its tracks?I just tried this and it works just fine: def test render :text => ''foo'' return fail ''this should raise an error'' end If you comment out the "return" an error will be raised from the "fail" method. It looks right your doing it the right way. Just remember that puts in your controller does not write anything to your rendered HTML you see in the browser. -- Posted via http://www.ruby-forum.com/.
Kevin Olbrich
2006-Jul-27 22:36 UTC
[Rails] Re: How do you stop a Ruby method from executing?
On Friday, July 28, 2006, at 12:18 AM, Alex Wayne wrote:>Eric Gross wrote: >> Hey guys, >> >> How do you stop Ruby code from executing? For example, >> >> def newaction >> puts "1" >> return >> puts "2" >> end >> >> >> I tried return in the example above, but it continues and than looks for >> newaction.rhtml. How do I stop it completely dead in its tracks? > >I just tried this and it works just fine: > > def test > render :text => ''foo'' > return > fail ''this should raise an error'' > end > >If you comment out the "return" an error will be raised from the "fail" >method. It looks right your doing it the right way. > >Just remember that puts in your controller does not write anything to >your rendered HTML you see in the browser. > >-- >Posted via http://www.ruby-forum.com/. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/railsRails overrides does some default processing when an action returns. One of those things is to render the default view unless a render has been called. So unless you redirect, it will fall through to the view. _Kevin www.sciwerks.com -- Posted with http://DevLists.com. Sign up and save your mailbox.
Eric Gross
2006-Jul-27 22:58 UTC
[Rails] Re: How do you stop a Ruby method from executing?
Hey, I tried out your code. That doesnt give me the desired result. I just want the focus to return to the current page. If I have def tester end I dont want it to go to tester.rhtml or redirect to a different page. I just want it to completely halt or stop and return to the page with nothing changed on the page. "Return" doesn''t work for this and neither does render :nothing => true. Is there anyway to stop Ruby code. It just seems like such an important feature... -- Posted via http://www.ruby-forum.com/.
Alex Wayne
2006-Jul-27 23:01 UTC
[Rails] Re: How do you stop a Ruby method from executing?
Eric Gross wrote:> Hey, > > I tried out your code. That doesnt give me the desired result. > > I just want the focus to return to the current page. If I have > > def tester > > end > > I dont want it to go to tester.rhtml or redirect to a different page. I > just want it to completely halt or stop and return to the page with > nothing changed on the page. "Return" doesn''t work for this and neither > does render :nothing => true. > > Is there anyway to stop Ruby code. It just seems like such an important > feature...Oh, return wont work in that case because it simply halts execution of the current method. Not the entire rails app. You just just abort in this way becuase the browser has already requested the new page. At this point you have to do a "redirect_to" back to the page you just came form. -- Posted via http://www.ruby-forum.com/.
Eric Gross
2006-Jul-27 23:04 UTC
[Rails] Re: How do you stop a Ruby method from executing?
if I redirect back to the page we came from, then all the info that was there will be deleted on the screen refresh. So basically there is no way to stop the ruby execution. That seems kinda weird...> > Oh, return wont work in that case because it simply halts execution of > the current method. Not the entire rails app. > > You just just abort in this way becuase the browser has already > requested the new page. At this point you have to do a "redirect_to" > back to the page you just came form.-- Posted via http://www.ruby-forum.com/.
Alex Wayne
2006-Jul-27 23:11 UTC
[Rails] Re: How do you stop a Ruby method from executing?
Eric Gross wrote:> if I redirect back to the page we came from, then all the info that was > there will be deleted on the screen refresh. So basically there is no > way to stop the ruby execution. That seems kinda weird... >> >> Oh, return wont work in that case because it simply halts execution of >> the current method. Not the entire rails app. >> >> You just just abort in this way becuase the browser has already >> requested the new page. At this point you have to do a "redirect_to" >> back to the page you just came form.The problem is the browser, not ruby or rails. Think fo the following conversation between browser and server: Browser: get me the page ''/post/edit/1'' Server: here you go: <html...> Browser: ok, show the page ''/post/update/1'' with the attached form data Server: (halts mid process) here''s a blank page Browser: ok, guess I''ll show this blank page. The way to do what you want is either via ajax, or how the standard scaffold handles it by stuff the form data into a variable, and then redisplaying the same form with it so the fields have the proper values. -- Posted via http://www.ruby-forum.com/.
Jason Edgecombe
2006-Jul-27 23:24 UTC
[Rails] Re: How do you stop a Ruby method from executing?
Eric Gross wrote:> if I redirect back to the page we came from, then all the info that was > there will be deleted on the screen refresh. So basically there is no > way to stop the ruby execution. That seems kinda weird... > >> Oh, return wont work in that case because it simply halts execution of >> the current method. Not the entire rails app. >> >> You just just abort in this way becuase the browser has already >> requested the new page. At this point you have to do a "redirect_to" >> back to the page you just came form. >> > > >I suggest that you capture the form post and pass it to the page that you redirect to. You could also save it in the session and just display the data from the form. Jason
Trevor Squires
2006-Jul-28 00:07 UTC
[Rails] Re: How do you stop a Ruby method from executing?
On 27-Jul-06, at 4:11 PM, Alex Wayne wrote:> Eric Gross wrote: >> if I redirect back to the page we came from, then all the info >> that was >> there will be deleted on the screen refresh. So basically there is no >> way to stop the ruby execution. That seems kinda weird... >>> >>> Oh, return wont work in that case because it simply halts >>> execution of >>> the current method. Not the entire rails app. >>> >>> You just just abort in this way becuase the browser has already >>> requested the new page. At this point you have to do a >>> "redirect_to" >>> back to the page you just came form. > > The problem is the browser, not ruby or rails. Think fo the following > conversation between browser and server: > > Browser: get me the page ''/post/edit/1'' > Server: here you go: <html...> > Browser: ok, show the page ''/post/update/1'' with the attached form > data > Server: (halts mid process) here''s a blank page > Browser: ok, guess I''ll show this blank page. > > The way to do what you want is either via ajax, or how the standard > scaffold handles it by stuff the form data into a variable, and then > redisplaying the same form with it so the fields have the proper > values. >While I question the usability of "just halting" as though the user didn''t click a link or press a button (no feedback, which is bad), it *is* possible to do this without redirects or ajax. If you take a look at the HTTP spec you''ll see that the http response codes of 204 and 205 tell the browser not to expect new content in the response. So, in your controller you do: return render(:nothing => true, :status => 204) Hope this helps, Trevor -- Trevor Squires http://somethinglearned.com> -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Eric Gross
2006-Jul-28 01:53 UTC
[Rails] Re: Re: How do you stop a Ruby method from executing?
Trevor, That works like a charm!!! Thanks you so much! Eric -- Posted via http://www.ruby-forum.com/.
Eric Gross
2006-Jul-28 02:09 UTC
[Rails] Re: Re: How do you stop a Ruby method from executing?
Hey Trevor, Do you happen to know how I could throw up a popup window, before the return render command? It could be javascript or whatever. Eric -- Posted via http://www.ruby-forum.com/.
you can do: def test @show_prompt_window = true return @show_prompt_window = false end then have it fall to an foo.rjs and in foo.rjs if @show_prompt_window use some js magic else render a partial cheers Eric Gross wrote:> Hey Trevor, > > Do you happen to know how I could throw up a popup window, before the > return render command? It could be javascript or whatever. > > Eric-- Posted via http://www.ruby-forum.com/.
Eric Gross
2006-Jul-28 02:37 UTC
[Rails] Re: Re: How do you stop a Ruby method from executing?
Ok, Im a little new do rjs, do basically I have def test return render(:nothing => true, :status => 204) end Where return render(:nothing => true, :status => 204) returns me to the window with nothing done. How should I modify that to access and rjs file? Erich wrote:> you can do: > > def test > > @show_prompt_window = true > > return > @show_prompt_window = false > end > > then have it fall to an foo.rjs > > and in foo.rjs > > if @show_prompt_window > use some js magic > else > render a partial > > cheers > > Eric Gross wrote: >> Hey Trevor, >> >> Do you happen to know how I could throw up a popup window, before the >> return render command? It could be javascript or whatever. >> >> Eric-- Posted via http://www.ruby-forum.com/.
Eric Gross
2006-Jul-28 02:38 UTC
[Rails] Re: Re: How do you stop a Ruby method from executing?
Ok, Im a little new do rjs, so basically I have def test return render(:nothing => true, :status => 204) end Where return render(:nothing => true, :status => 204) returns me to the window with nothing done. How should I modify that to access and rjs file? Erich wrote:> you can do: > > def test > > @show_prompt_window = true > > return > @show_prompt_window = false > end > > then have it fall to an foo.rjs > > and in foo.rjs > > if @show_prompt_window > use some js magic > else > render a partial > > cheers > > Eric Gross wrote: >> Hey Trevor, >> >> Do you happen to know how I could throw up a popup window, before the >> return render command? It could be javascript or whatever. >> >> Eric-- Posted via http://www.ruby-forum.com/.
I think you will have to remove the render (:nothing ...) . YOu want it to fall to the rjs file. prepend your current rhtml file with an _. Then just use the logic i described. call the partial (the file with the _) if you want a normal view or if you rather have the page display some prompt window, use some js magic for that Eric Gross wrote:> Ok, Im a little new do rjs, so basically I have > > def test > return render(:nothing => true, :status => 204) > end > > > Where return render(:nothing => true, :status => 204) returns me to the > window with nothing done. How should I modify that to access and rjs > file? > > > Erich wrote: >> you can do: >> >> def test >> >> @show_prompt_window = true >> >> return >> @show_prompt_window = false >> end >> >> then have it fall to an foo.rjs >> >> and in foo.rjs >> >> if @show_prompt_window >> use some js magic >> else >> render a partial >> >> cheers >> >> Eric Gross wrote: >>> Hey Trevor, >>> >>> Do you happen to know how I could throw up a popup window, before the >>> return render command? It could be javascript or whatever. >>> >>> Eric-- Posted via http://www.ruby-forum.com/.
Kevin Olbrich
2006-Jul-28 02:59 UTC
[Rails] Re: How do you stop a Ruby method from executing?
On Friday, July 28, 2006, at 12:58 AM, Eric Gross wrote:>Hey, > >I tried out your code. That doesnt give me the desired result. > >I just want the focus to return to the current page. If I have > >def tester > >end > >I dont want it to go to tester.rhtml or redirect to a different page. I >just want it to completely halt or stop and return to the page with >nothing changed on the page. "Return" doesn''t work for this and neither >does render :nothing => true. > >Is there anyway to stop Ruby code. It just seems like such an important >feature... > >-- >Posted via http://www.ruby-forum.com/. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/railstry... def action redirect_to :back end _Kevin www.sciwerks.com -- Posted with http://DevLists.com. Sign up and save your mailbox.
Trevor Squires
2006-Jul-28 03:23 UTC
[Rails] Re: Re: How do you stop a Ruby method from executing?
On 27-Jul-06, at 7:08 PM, Eric Gross wrote:> Hey Trevor, > > Do you happen to know how I could throw up a popup window, before the > return render command? It could be javascript or whatever. >Aha! so you actually do want some user feedback (rather than just stop processing and do nothing) then... Okay, if what you want is to have the browser pop up a window in response to the user click *every* time they click, then it''s just a case of adding an onclick="alert(''I am going to talk to the server now!!!'')" sort of thing to your link/button. However, if your controller''s action will decide whether or not to popup for a given request then you''re getting into the realms of ajax and the like - and you aren''t actually simply halting processing of an action. Which means that the advice other people gave about redirects and ajax are more relevant to your needs. In that case I''d suggest you lock yourself into a room a read some ajax tutorials, as well as the ajax sections of the AWDR book and the ajax related recipes from the Rails Recipes book. You need to have a grounding in how things are supposed to work before the question/ answer banter of a mailing list will be effective for you. HTH, Trevor> Eric > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails