Get target path before authentication

I'm using Devise for user authentication in a rails application, and so certain routes are wrapped in a authenticate tag to allow access only to users who are logged in.

authenticate :user do
    post '/user/orders/new', to: 'user/orders#new', as: :new_user_order
end

For example, when a user tries to create a new order when they're not logged in, they're redirected to a sign in page.

What I want is for the user to be directed to the new order page after logging in. Currently, we're using request.referrer which redirects the user back to the page they were on before trying to create a new order. I can't seem to figure out a way of getting the path they were targeting before being sent to sign in.

Anybody know how I could do this?

Thanks

Answers


You need to provide a before filter for user authentication in the controller and not mention it in routes . Just put the following code in the controller and it should redirect to sign-up if the user isn't logged in -

 before_filter :authenticate_user

You need to override Devise's after_sign_in_path_for(resource_or_scope) method, you can do it in application controller

def after_sign_in_path_for(resource_or_scope)
  # new_order_path
end

Hope that helped!


You can persist the last url - in session[:bookmark] and by doing something like this -

prepend_before_filter :set_bookmark

def set_bookmark
  session[:bookmark] = request.url
end

And within SessionsController, you can use value of session[:bookmark] if it exists or after_sign_in_path_for


Need Your Help

GitHub API to Create a File

git curl github github-api http-put

So I'm trying to familiarize myself with the GitHub API. I'm using cURL commands to implement some of their basic functionality. I can get the basic authorization & repository creation correctly.

Animated UIButtons snap back to original position after opening UIAlertView

ios objective-c animation uiview uialertview

I am making a Welcome screen with two UITextFields, a Forgot Password button and two additional buttons (signInButton and createAccountButton). These two buttons are placed in an UIView (inputsView)