# Return from an action

**URL:** <https://www.ruby-forum.com/t/return-from-an-action/66366>\
**Category:** Rails\
**Created:** [August 24, 2006, 11:49pm UTC](https://www.ruby-forum.com/t/return-from-an-action/66366 "2006-08-24T23:49:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@Mage](https://www.ruby-forum.com/u/Mage)\
**Post date:** [August 24, 2006, 11:49pm UTC](https://www.ruby-forum.com/t/return-from-an-action/66366/1 "2006-08-24T23:49:21Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![Mage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@Mage](https://www.ruby-forum.com/u/Mage)\
**Post date:** [August 25, 2006, 12:11am UTC](https://www.ruby-forum.com/t/return-from-an-action/66366/2 "2006-08-25T00:11:23Z")

</div>

On 8/24/06, Mage [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> “other things”?
> 
> I could use:  
> requires\_valid\_login or return

Use a before\_filter and return false, aborting the action.

jeremy

---

<div class="post-metadata">

**Author:** ![Mage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@Mage](https://www.ruby-forum.com/u/Mage)\
**Post date:** [August 25, 2006, 12:09am UTC](https://www.ruby-forum.com/t/return-from-an-action/66366/3 "2006-08-25T00:09:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@Mage](https://www.ruby-forum.com/u/Mage)\
**Post date:** [August 25, 2006, 12:33am UTC](https://www.ruby-forum.com/t/return-from-an-action/66366/4 "2006-08-25T00:33:26Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![Mage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@Mage](https://www.ruby-forum.com/u/Mage)\
**Post date:** [August 25, 2006, 12:35am UTC](https://www.ruby-forum.com/t/return-from-an-action/66366/5 "2006-08-25T00:35:27Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![Mage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@Mage](https://www.ruby-forum.com/u/Mage)\
**Post date:** [August 25, 2006, 6:01pm UTC](https://www.ruby-forum.com/t/return-from-an-action/66366/6 "2006-08-25T18:01:44Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![Mage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@Mage](https://www.ruby-forum.com/u/Mage)\
**Post date:** [September 4, 2006, 2:46pm UTC](https://www.ruby-forum.com/t/return-from-an-action/66366/7 "2006-09-04T14:46:10Z")

</div>

Uhhhh to keep the syntax ruby-like you should rename valid\_login to  
valid\_login?

---

<div class="post-metadata">

**Author:** ![Mage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@Mage](https://www.ruby-forum.com/u/Mage)\
**Post date:** [August 25, 2006, 4:33pm UTC](https://www.ruby-forum.com/t/return-from-an-action/66366/8 "2006-08-25T16:33:35Z")

</div>

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
