I have an ajax voting button: If a user clicks on a \"thumbs up\" image, but is not logged in, then they should see a dialog box that asks them to login first.
To do thi
use a cookie. Get the window.location.href that they are trying to go to and store it as a cookie. On your home page, check for the presence of this cookier in the document ready function, redirect if its there
I do the following with Authlogic on Rails 3, perhaps you could use it or do something similar:
class ApplicationController < ActionController::Base
after_filter :store_location
private
def store_location
session[:return_to] = request.fullpath
end
def clear_stored_location
session[:return_to] = nil
end
def redirect_back_or_to(alternate)
redirect_to(session[:return_to] || alternate)
clear_stored_location
end
end
Then you can use something like redirect_back_or_to dashboard_path in your login action.
For some controllers/actions, I don't want to store the location (e.g. the login page). For those I just skip the filter:
class UserController < ApplicationController
skip_after_filter :store_location
end
There's also a page in the Devise wiki on doing this: https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in
In ApplicationController write following:
after_filter :store_location
def store_location
session[:previous_urls] ||= []
# store unique urls only
session[:previous_urls].prepend request.fullpath if session[:previous_urls].first != request.fullpath
# For Rails < 3.2
# session[:previous_urls].unshift request.fullpath if session[:previous_urls].first != request.fullpath
session[:previous_urls].pop if session[:previous_urls].count > 2
end
def after_sign_in_path_for(resource)
session[:previous_urls].last || root_path
end
Source: https://github.com/plataformatec/devise/wiki/How-To:-Redirect-back-to-current-page-after-sign-in,-sign-out,-sign-up,-update