Return from an action

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

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

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

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

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

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]

Uhhhh to keep the syntax ruby-like you should rename valid_login to
valid_login?

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