Mage
August 24, 2006, 11:49pm
1
Hello,
I would like to exit an action, however, I cannot use return because it
should be called from another method:
def click
requires_valid_login
other_things
end
protected
def requires_valid_login
if !@user
flash[:error] = ‘you must be logged for view this page’
redirect_to :back
end
end
Is there any elegant way to exit from action :click and don’t process
“other things”?
I could use:
requires_valid_login or return
However I don’t like this.
Mage
Mage
August 25, 2006, 12:11am
2
On 8/24/06, Mage [email protected] wrote:
end
“other things”?
I could use:
requires_valid_login or return
Use a before_filter and return false, aborting the action.
jeremy
Mage
August 25, 2006, 12:09am
3
Hope this helps…
def click
return unless valid_login
other_things
end
def valid_login
if !@user
flash[:error] = ‘you must be logged for view this page’
redirect_to :back
false
end
true
end
Mage
August 25, 2006, 12:33am
4
Jeremy K. wrote:
Use a before_filter and return false, aborting the action.
Thank you, I am already using before filter which does the
authentication, however I don’t want to abort every actions, only some
of them.
Is it possible to abort the action inside from a method called by the
action itself?
Mage
Mage
August 25, 2006, 12:35am
5
Carl F. wrote:
Hope this helps…
def click
return unless valid_login
other_things
end
That’s what I am trying to avoid, the explicit return from the action
itself.
Mage
Mage
August 25, 2006, 6:01pm
6
Al Evans wrote:
Mage wrote:
Jeremy K. wrote:
Use a before_filter and return false, aborting the action.
Thank you, I am already using before filter which does the
authentication, however I don’t want to abort every actions, only some
of them.
For example:
before_filter :valid_login, :except => :login
–Al Evans
Yes before_filter accepts some options that let you skip filters on
certain actions. :except is one, and :only is the other. You can give
it a single action name or an array of action names.
before_filter :valid_login, :only => [:protected_one, :protected_two]
Mage
September 4, 2006, 2:46pm
7
Uhhhh to keep the syntax ruby-like you should rename valid_login to
valid_login?
Mage
August 25, 2006, 4:33pm
8
Mage wrote:
Jeremy K. wrote:
Use a before_filter and return false, aborting the action.
Thank you, I am already using before filter which does the
authentication, however I don’t want to abort every actions, only some
of them.
For example:
before_filter :valid_login, :except => :login
–Al Evans