# Error in the code Doube Render (colins')

**URL:** <https://www.ruby-forum.com/t/error-in-the-code-doube-render-colins/185504>\
**Category:** Rails\
**Created:** [March 21, 2010, 8:00pm UTC](https://www.ruby-forum.com/t/error-in-the-code-doube-render-colins/185504 "2010-03-21T20:00:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![guru\_101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/77aa72/32.png) [@guru\_101](https://www.ruby-forum.com/u/guru_101)\
**Post date:** [March 21, 2010, 8:00pm UTC](https://www.ruby-forum.com/t/error-in-the-code-doube-render-colins/185504/1 "2010-03-21T20:00:40Z")

</div>

Hi everyone, i followed colins example and it worked perfectly but now i  
get this error

ActionController::DoubleRenderError in UserController#login

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”.

I feel that something is wrong with this code

def login  
@title = “Login - Books”

```
if request.post?
  @user = User.new(params[:user])
  user = User.find_by_screen_name_and_password(@user.screen_name,

```

@user.password)  
if user  
session[:user\_id] = user.id  
flash[:notice] = “User #{user.screen\_name} logged in”  
redirect\_to :action =\> “index”  
else  
# Don’t show the password in the view  
@user.password = nil  
flash[:notice] = “Invalid email/password combination”  
end  
end

```
 if session[:user_id]
  flash[:notice] = "Already registed and logged in! You cannot view

```

the register or login!"  
redirect\_to :action =\> :index  
end  
end

///////////////////////////////////////////////////////

```
 if session[:user_id]
  flash[:notice] = "Already registed and logged in! You cannot view

```

the register or login!"  
redirect\_to :action =\> :index  
end

///////////////////////////////////////////////////////

what this does, is if session ID is found, then it takes the login page  
back to index…can someone help me please ☹

---

<div class="post-metadata">

**Author:** ![guru\_101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/77aa72/32.png) [@guru\_101](https://www.ruby-forum.com/u/guru_101)\
**Post date:** [March 21, 2010, 8:18pm UTC](https://www.ruby-forum.com/t/error-in-the-code-doube-render-colins/185504/2 "2010-03-21T20:18:12Z")

</div>

On Mar 21, 7:00 pm, Tiger Big one [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

In the case where the request is a post request and if the user is  
logged in successfully you are calling redirect\_to :action =\> ‘index’  
twice

Fred

---

<div class="post-metadata">

**Author:** ![guru\_101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/77aa72/32.png) [@guru\_101](https://www.ruby-forum.com/u/guru_101)\
**Post date:** [March 21, 2010, 8:21pm UTC](https://www.ruby-forum.com/t/error-in-the-code-doube-render-colins/185504/3 "2010-03-21T20:21:17Z")

</div>

Frederick C. wrote:

> On Mar 21, 7:00ï¿½pm, Tiger Big one [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > 
> 
> In the case where the request is a post request and if the user is  
> logged in successfully you are calling redirect\_to :action =\> ‘index’  
> twice
> 
> Fred

I wrote the same code (exactly the same code)…twice for def login and  
def register.

Does that matter?

---

<div class="post-metadata">

**Author:** ![guru\_101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/77aa72/32.png) [@guru\_101](https://www.ruby-forum.com/u/guru_101)\
**Post date:** [March 22, 2010, 11:40am UTC](https://www.ruby-forum.com/t/error-in-the-code-doube-render-colins/185504/4 "2010-03-22T11:40:08Z")

</div>

On 21 March 2010 19:21, Tiger Big one [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def register.
> 
> Does that matter?

The answer is obvious, if it is exactly the same and you hit the right  
path through the code then it will fail, so yes it does matter.  
The reason there is a problem is that, rather non-intuitively,  
redirect\_to is not a jump but a call, therefore execution returns  
after the redirect\_to. You must make sure there is not another  
redirect. The safest thing is to put a return after redirect\_to if it  
is in the middle of the action rather than the end.

Colin
