# Evaluating rubycode after redirect

**URL:** <https://www.ruby-forum.com/t/evaluating-rubycode-after-redirect/130680>\
**Category:** Rails\
**Created:** [March 1, 2008, 7:47pm UTC](https://www.ruby-forum.com/t/evaluating-rubycode-after-redirect/130680 "2008-03-01T19:47:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![der\_guido](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@der\_guido](https://www.ruby-forum.com/u/der_guido)\
**Post date:** [March 1, 2008, 7:47pm UTC](https://www.ruby-forum.com/t/evaluating-rubycode-after-redirect/130680/1 "2008-03-01T19:47:22Z")

</div>

Hi,

there is somthing, that I do not understand.

class MyController \< ApplicationController

def index  
…  
end

def my\_def  
redirect\_to :action=\> “index” unless params[:id]

```
my_model = MyModel.find(params[:id])
...

```

end  
end

when I try to call “my\_controller/my\_def” without an id in y browser,  
then the find-statement will is evaluated ☹ why?!?

thanks Guido

---

<div class="post-metadata">

**Author:** ![der\_guido](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@der\_guido](https://www.ruby-forum.com/u/der_guido)\
**Post date:** [March 1, 2008, 8:18pm UTC](https://www.ruby-forum.com/t/evaluating-rubycode-after-redirect/130680/2 "2008-03-01T20:18:24Z")

</div>

> when I try to call “my\_controller/my\_def” without an id in y browser,  
> then the find-statement will is evaluated ☹ why?!?
> 
> thanks Guido

because redirect\_to is not a return… it’s a method call… use the  
following form:

def my\_def  
if params[:id]  
my\_model = MyModel.find params[:id]  
else  
redirect\_to :action =\> “index”  
end  
end
