Redirect_to not working

Hi… I have a basic authentication system. I have a RESTful resource
called session with the following code when logging in:

def new
end

def create
user = User.authenticate(params[:username], params[:password])

respond_to do |format|
  if user
    session[:user_id] = user.id
    flash[:notice] = "Welcome back #{user.name}!"
    format.html {redirect_to (:controller => 'welcome')}
  else
    flash[:error] = "The username and/or password is invalid!"
    format.html { render :action => :new }
    format.js
  end
end

end

when logging in I get the following in the console:

Processing SessionsController#create (for 127.0.0.1 at 2008-07-01
09:58:03) [POST]
Session ID:
BAh7BzoMY3NyZl9pZCIlYjA3ZGQwMTVmZGU0YWQwMzZiY2U5MGRkODM2YThl
YTciCmZsYXNoSUM6J0FjdGlvbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhh
c2h7AAY6CkB1c2VkewA=–7162c9867e535bbfe83e6de00ed23348f1c58c25
Parameters: {“commit”=>“Log in”, “action”=>“create”,
“authenticity_token”=>“ccb053521b704dcd80842248e30717f8ecf1cb27”,
“username”=>“nateleavitt”, “controller”=>“sessions”,
“password”=>"[FILTERED]"}
User Columns (0.003677) SHOW FIELDS FROM users
User Load (0.002006) SELECT * FROM users WHERE (users.username
= ‘nateleavitt’ AND users.password =
‘d3b13af68983849e6d7e944bde7d02fffc38bf2a62a9d7f75d025a62a1af67d6’)
LIMIT 1
Redirected to http://localhost:3000/welcome
Completed in 0.04638 (21 reqs/sec) | DB: 0.00568 (12%) | 302 Found
[http://localhost/session]

Processing WelcomeController#index (for 127.0.0.1 at 2008-07-01
09:58:03) [GET]
Session ID:
BAh7CDoMY3NyZl9pZCIlYjA3ZGQwMTVmZGU0YWQwMzZiY2U5MGRkODM2YThl
YTc6DHVzZXJfaWRpBiIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxh
c2g6OkZsYXNoSGFzaHsGOgtub3RpY2UiIVdlbGNvbWUgYmFjayBOYXRoYW4g
TGVhdml0dCEGOgpAdXNlZHsGOwhG–2ad40b26ad78c3a7aec22ae0d05b647e64a9d16e
Parameters: {“action”=>“index”, “controller”=>“welcome”}
Rendering template within layouts/application
Rendering welcome/index
Rendered shared/_tabs (0.00261)
Completed in 0.01578 (63 reqs/sec) | Rendering: 0.00692 (43%) | DB:
0.00000 (0%) | 200 OK [http://localhost/welcome]

As you can see… it renders the welcome controller and action, but on
my browser it’s still at the login screen. I can’t figure out why it
isn’t redirecting me to the welcome/ page. Any ideas?

Oh… and here is the code for the actual form:

<% remote_form_for :session, :url => session_path do -%>
  <fieldset style="border:0px;">
    <p>
      <label for="username" class="required">Username</label>
      <%= text_field_tag :username, params[:username] %>
    </p>
    <p>
      <label for="password" class="required">Password</label>
      <%= password_field_tag :password, params[:password] %>
    </p>
    <p>
    <%= submit_tag 'Log in' %>
    </p>
  </fieldset>
<% end -%>

You’re using remote_form_for, which expects an Ajax response.
I guess your welcome/index action renders simple html instead

let me explain a little more how I’m using the remote_form_for… then
maybe I’ll get better suggestions:

I have this code at the top of the form:

<% if flash[:error] %><%= flash[:error] %><% end %>

then the create.rjs has this:

page.visual_effect :highlight, :error
page.replace_html :error, flash[:error]
flash.discard

is there anyway to accomplish the same effect for the login using an rjs
template… seeing as how I’ll need to forward after logging in?

On 1 Jul 2008, at 18:13, Nate L. wrote:

Thorsten M. wrote:

You’re using remote_form_for, which expects an Ajax response.
I guess your welcome/index action renders simple html instead

Ok… so the remote_form_for always expects an Ajax response?.. If so,
then how would I accomplish the redirect? I was using the
remote_form_for to show the error messages.

When an ajax request gets redirected, the javascript never sees the
redirect, just the final content.
If you;re using ajax with a :update parameter (ie generating an
Ajax.Updater) then without extra client side code you’re boned.
If you’re not (ie you’re using rjs), then you can use page.redirect_to
inside your rjs file (which just generates a window.location=…)

Fred

Thorsten M. wrote:

You’re using remote_form_for, which expects an Ajax response.
I guess your welcome/index action renders simple html instead

Ok… so the remote_form_for always expects an Ajax response?.. If so,
then how would I accomplish the redirect? I was using the
remote_form_for to show the error messages.

hmm… is there no way to use the remote_form_for, then redirect?.. I’m
assuming that wouldn’t be very clean? any ideas?

Frederick C. wrote:

On 1 Jul 2008, at 18:13, Nate L. wrote:

Thorsten M. wrote:

You’re using remote_form_for, which expects an Ajax response.
I guess your welcome/index action renders simple html instead

Ok… so the remote_form_for always expects an Ajax response?.. If so,
then how would I accomplish the redirect? I was using the
remote_form_for to show the error messages.

When an ajax request gets redirected, the javascript never sees the
redirect, just the final content.
If you;re using ajax with a :update parameter (ie generating an
Ajax.Updater) then without extra client side code you’re boned.
If you’re not (ie you’re using rjs), then you can use page.redirect_to
inside your rjs file (which just generates a window.location=…)

Fred

Thanks for the replies guys! I understand what you guys are saying. I
guess I’m confused as to the best way to implement. Or even if I should
implement on the login anyways. Cause if you look at my create action
on the sesion controller there really wouldn’t be a way for me to use
another rjs… cause the one i’m using create.rjs is being used as an
error view.

Any suggestions?

On 1 Jul 2008, at 18:30, Nate L. wrote:

so,

Fred

Thanks for the replies guys! I understand what you guys are saying. I
guess I’m confused as to the best way to implement. Or even if I
should
implement on the login anyways. Cause if you look at my create action
on the sesion controller there really wouldn’t be a way for me to use
another rjs… cause the one i’m using create.rjs is being used as an
error view.

You can render a different template, use a render :update block etc…

Fred

Frederick C. wrote:

On 1 Jul 2008, at 18:30, Nate L. wrote:

so,

Fred

Thanks for the replies guys! I understand what you guys are saying. I
guess I’m confused as to the best way to implement. Or even if I
should
implement on the login anyways. Cause if you look at my create action
on the sesion controller there really wouldn’t be a way for me to use
another rjs… cause the one i’m using create.rjs is being used as an
error view.

You can render a different template, use a render :update block etc…

Fred

So would I do this in the session controller, create action? Is this
what you are suggesting?

def create
user = User.authenticate(params[:username], params[:password])

respond_to do |format|
  if user
    session[:user_id] = user.id
    flash[:notice] = "Welcome back #{user.name}!"
    format.html {render :action => :redirect}

  else
    flash[:error] = "The username and/or password is invalid!"
    format.html { render :action => :new }
    format.js
  end
end

end

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