How do you stop a Ruby method from executing?

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?

Eric G. 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.

On Friday, July 28, 2006, at 12:18 AM, Alex W. wrote:

end
Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Rails 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

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…

Eric G. 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.

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.

Eric G. 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.

Eric G. wrote:

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

On 27-Jul-06, at 4:11 PM, Alex W. wrote:

You just just abort in this way becuase the browser has already
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 S.
http://somethinglearned.com

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

Trevor,

That works like a charm!!! Thanks you so much!

Eric

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

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

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

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

On Friday, July 28, 2006, at 12:58 AM, Eric G. wrote:

I dont want it to go to tester.rhtml or redirect to a different page. I
Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

try…

def action
redirect_to :back
end

_Kevin
www.sciwerks.com

On 27-Jul-06, at 7:08 PM, Eric G. 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs