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";
      }
    );
geeky_monster
  • 8,672
  • 18
  • 55
  • 86
  • 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 09 '11 at 09:42
  • Thanks . But changing it to users_url did not help .I just edited the question to add an extract from the UsersController. – geeky_monster Jun 09 '11 at 10:05
  • @mad.geek: this is a facebook signin right? Is it occurring via AJAX or standard HTML posts? – Jesse Wolgamott Jun 09 '11 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 ? – geeky_monster Jun 09 '11 at 16:32
  • I added the AJAX code in the question. – geeky_monster Jun 09 '11 at 16:43
  • The issue seems to get fixed if I do a javascript redirect via a callback function inside the $post(). – geeky_monster Jun 09 '11 at 17:10

2 Answers2

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);
    }
}

});

Community
  • 1
  • 1
Jesse Wolgamott
  • 40,197
  • 4
  • 83
  • 109
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}}); 
geeky_monster
  • 8,672
  • 18
  • 55
  • 86
Austin Ginder
  • 516
  • 3
  • 5
  • 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 . – geeky_monster Jun 09 '11 at 09:59