Error in the code Doube Render (colins')

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 :frowning:

On Mar 21, 7:00 pm, Tiger Big one [email protected] 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

Frederick C. wrote:

On Mar 21, 7:00�pm, Tiger Big one [email protected] 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?

On 21 March 2010 19:21, Tiger Big one [email protected] 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs