# Exiting the function

**URL:** <https://www.ruby-forum.com/t/exiting-the-function/187566>\
**Category:** Rails\
**Created:** [April 26, 2010, 2:17am UTC](https://www.ruby-forum.com/t/exiting-the-function/187566 "2010-04-26T02:17:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohammed\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bcef8e/32.png) [@mohammed\_a](https://www.ruby-forum.com/u/mohammed_a)\
**Post date:** [April 26, 2010, 2:17am UTC](https://www.ruby-forum.com/t/exiting-the-function/187566/1 "2010-04-26T02:17:38Z")

</div>

Hi

I have this code  
def destroy  
@property = Property.find(params[:id])  
IsAuthorized?(@property.user\_id)  
@property.destroy

```
respond_to do |format|
  format.html { redirect_to(properties_url) }
  format.xml { head :ok }
end

```

end

def IsAuthorized?(id)  
if current\_user.id!= id  
flash[:notice] = 'Not authorized ’  
redirect\_to(properties\_url)  
end  
end

If a not authorized user calls destroy it stills calls  
@property.destroy.  
How can I prevent the destory function from calling @property.destroy  
if the user is not authorized?

---

<div class="post-metadata">

**Author:** ![mohammed\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bcef8e/32.png) [@mohammed\_a](https://www.ruby-forum.com/u/mohammed_a)\
**Post date:** [April 26, 2010, 2:21am UTC](https://www.ruby-forum.com/t/exiting-the-function/187566/2 "2010-04-26T02:21:43Z")

</div>

On Sun, Apr 25, 2010 at 5:16 PM, Mohammed A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi
> 
> I have this code  
> def destroy  
> @property = Property.find(params[:id])  
> IsAuthorized?(@property.user\_id)  
> @property.destroy

Mohammed, you can write the above as follows:

@property.destroy if IsAuthorized?(@property.user\_id)

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![mohammed\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bcef8e/32.png) [@mohammed\_a](https://www.ruby-forum.com/u/mohammed_a)\
**Post date:** [April 26, 2010, 3:25am UTC](https://www.ruby-forum.com/t/exiting-the-function/187566/3 "2010-04-26T03:25:31Z")

</div>

On Sun, Apr 25, 2010 at 5:20 PM, Conrad T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Mohammed, you can write the above as follows:  
> @property.destroy if IsAuthorized?(@property.user\_id)

Well, you could, _if_ IsAuthorized? – which idiomatically should  
be is\_authorized? – returned a boolean value 🙂

> > def IsAuthorized?(id)  
> > if current\_user.id!= id  
> > flash[:notice] = 'Not authorized ’  
> > redirect\_to(properties\_url)  
> > end  
> > end

e.g.

def is\_authorized?(id)  
current\_user.id == id  
end

–  
Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![mohammed\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bcef8e/32.png) [@mohammed\_a](https://www.ruby-forum.com/u/mohammed_a)\
**Post date:** [April 26, 2010, 2:10pm UTC](https://www.ruby-forum.com/t/exiting-the-function/187566/4 "2010-04-26T14:10:32Z")

</div>

Conrad

I did you suggested but I got this message  
Render and/or redirect were called multiple times in this action.  
Please note that you may only call render OR redirect, and at most  
once per action. Also note that neither redirect nor render terminate  
execution of the action, so if you want to exit an action after  
redirecting, you need to do something like “redirect\_to(…) and  
return”.

---

<div class="post-metadata">

**Author:** ![mohammed\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bcef8e/32.png) [@mohammed\_a](https://www.ruby-forum.com/u/mohammed_a)\
**Post date:** [April 26, 2010, 9:17am UTC](https://www.ruby-forum.com/t/exiting-the-function/187566/5 "2010-04-26T09:17:52Z")

</div>

On 26 April 2010 01:16, Mohammed A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> format.xml { head :ok }
> 
> ```
> 
> If a not authorized user calls destroy it stills calls  
> @property.destroy.

Adding to the other replies to this, the fundamental problem is that  
redirect\_to (somewhat non-intuitively) is not like a goto command. It  
initiates the redirect and then returns, so that IsAuthorized returns  
and the destroy is called whatever happened within IsAuthorized.

Colin

---

<div class="post-metadata">

**Author:** ![mohammed\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bcef8e/32.png) [@mohammed\_a](https://www.ruby-forum.com/u/mohammed_a)\
**Post date:** [April 27, 2010, 5:39pm UTC](https://www.ruby-forum.com/t/exiting-the-function/187566/6 "2010-04-27T17:39:07Z")

</div>

> > Mohammed, you can write the above as follows:  
> > @property.destroy if IsAuthorized?(@property.user\_id)
> 
> Well, you could, _if_ IsAuthorized? – which idiomatically should  
> be is\_authorized? – returned a boolean value 🙂

Personally I’d prefer authorized? - I never use is\_…?, the is\_ is just  
fluff (and doesn’t really make it any easier to read out  
loud/comprehend.

Cheers,

Andy

---

<div class="post-metadata">

**Author:** ![mohammed\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bcef8e/32.png) [@mohammed\_a](https://www.ruby-forum.com/u/mohammed_a)\
**Post date:** [April 26, 2010, 2:33pm UTC](https://www.ruby-forum.com/t/exiting-the-function/187566/7 "2010-04-26T14:33:22Z")

</div>

On 26 April 2010 13:09, Mohammed A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Conrad
> 
> I did you suggested but I got this message  
> Render and/or redirect were called multiple times in this action.  
> Please note that you may only call render OR redirect, and at most  
> once per action. Also note that neither redirect nor render terminate  
> execution of the action, so if you want to exit an action after  
> redirecting, you need to do something like “redirect\_to(…) and  
> return”.

Please put your replies inline rather than at the top of the message,  
then statements such as “I did as you suggested” make sense as they  
follow the suggestion made.

Read the other replies on this thread if you have not already done so.  
The problem, as explained in the error message above is that  
redirect\_to is not a goto command but a function call. Execution  
returns from there and drops through to the following code and so hits  
the respond\_to code and redirects again, which is illegal.

Colin

---

<div class="post-metadata">

**Author:** ![mohammed\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bcef8e/32.png) [@mohammed\_a](https://www.ruby-forum.com/u/mohammed_a)\
**Post date:** [April 27, 2010, 7:12pm UTC](https://www.ruby-forum.com/t/exiting-the-function/187566/8 "2010-04-27T19:12:40Z")

</div>

Andy J. wrote:

> > > Mohammed, you can write the above as follows:  
> > > @property.destroy if IsAuthorized?(@property.user\_id)
> > 
> > Well, you could, _if_ IsAuthorized? – which idiomatically should  
> > be is\_authorized? – returned a boolean value 🙂
> 
> Personally I’d prefer authorized? - I never use is\_…?, the is\_ is just  
> fluff (and doesn’t really make it any easier to read out  
> loud/comprehend.

Yes. Idiomatic Ruby style would be authorized? .

> Cheers,
> 
> Andy

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
