0

I have a rails app . I have created a sessionscontroller and want to redirect to users page '/users' once the user signs in. But the redirect doesnt seem to be happening.

class SessionsController < ApplicationController

  def create
     user = User.find_or_create_by_fbid(params[:user][:fbid]) #...Success
     user.update_attributes(params[:user])  #....Sucess
     sign_in(user)  # ....This occurs successfully 
     redirect_to users_path # .... Redirect doesnt occur on the browser side 
  end

end

The sign_in method is defined inside the Application Controller

class ApplicationController < ActionController::Base

  def sign_in(user)
    session[:fbid] = user.fbid
    @current_user = user
  end

end

Server Logs below . The redirect actually seems to be happening on the server side . But I do not see any change on the client side. The browser doesnt change page.

Server Logs

The UsersController

class UsersController < ApplicationController

  def index

    @users = User.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @users }
    end
  end

  end

Original Ajax Post -

$.post("/sessions",{user:{name:profile.name, email:profile.email,fbid:profile.id}}); 

The redirect occurs successfully if I use a javascript redirect statement inside the $post() as a callback function .

$.post("/sessions",{user:{name:profile.name, email:profile.email,fbid:profile.id}},function( data ) {
          window.location="/users";
      }
    );
6
  • I'm not sure if it is cause of the issue, but redirect should point to absolute URL, not relative one (so, you need to use ` redirect_to users_url`). Also, your controller code looks very optimistic.
    – taro
    Jun 9, 2011 at 9:42
  • Thanks . But changing it to users_url did not help .I just edited the question to add an extract from the UsersController. Jun 9, 2011 at 10:05
  • @mad.geek: this is a facebook signin right? Is it occurring via AJAX or standard HTML posts? Jun 9, 2011 at 14:53
  • @Jesse Wolgamott - Yes it is Facebook Sign in . The session#create ( Sign in ) is happening via an AJAX post after the user authenticates via Facebook. Are redirects not possible after AJAX POSTs ? Jun 9, 2011 at 16:32
  • I added the AJAX code in the question. Jun 9, 2011 at 16:43

2 Answers 2

1

You need to handle the response's redirection in an ajax query. Normally the browser handles, but it won't with ajax.

From this SO question (edited slightly for your case)

var params = {user:{name:profile.name, email:profile.email,fbid:profile.id}};
$.ajax({
type: "POST",
url: "/sessions",
data: params,
dataType: "json",
success: function(data, textStatus) {
    if (data.redirect) {
        // data.redirect contains the string URL to redirect to
        window.location.href = data.redirect;
    }
    else {
        // data.form contains the HTML for the replacement form
        $("#login_form").replaceWith(data.form);
    }
}

});

2
  • Yup. Got it . Thanks !! Any idea why redirect doesnt work in case of Ajax calls ? Jun 9, 2011 at 17:13
  • @mad.geek -- the browser isn't making the request, the javascript objects are -- and they don't redirect by default. Jun 9, 2011 at 19:01
0

I'm using a real basic authentication based of Ryan Bates' screencast. If you can't figure it out from my code, I recommend watching Authentication from Scratch

applications_controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery
  helper_method :current_user

  private

  def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end
end

sessions_controller.eb

class SessionsController < ApplicationController
  def new
  end

  def create
    user = User.authenticate(params[:email], params[:password])
    if user
      session[:user_id] = user.id
      redirect_to root_url, :notice => "Logged in!"
    else
      flash.now.alert = "Invalid email or password"
      render "new"
    end
  end

  def destroy
    session[:user_id] = nil
    redirect_to root_url, :notice => "Logged out!"
  end
end

Ajax Post -

$.post("/sessions",{user:{name:profile.name, email:profile.email,fbid:profile.id}}); 
1
  • If we take the bare bones, both are codes are the same. While you do a redirect to root_url , I am trying to do a redirect to users_url . Jun 9, 2011 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.