domingo, 10 de noviembre de 2013

Devise with Rails 4: Redirect back to current page after changing the password

Suppose that you are visiting a site, you are in an interesting page in this site and you need to sign up to continue doing things.

  This site is made in Rails 4 and use Devise for the authentication. If you want to continue in the same page that you were before the sign up, you need to implement in your application controller something similar to what is described here: How To: Redirect back to current page after sign in, sign out, sign up, update

  But imagine that your application has a "Forgot the password" feature. So you have the following URIs:

   - /users/password/new to ask for a new password
   - /users/password/edit in the email confirmation to change the password

  You'll have errors when you submit the password because the application will try to resend to the /users/password with a GET. But the URI /users/password is designed to be accessed only by PUT, so you'll get an error saying that the path /users/password with the method GET doesn't exist.

  To solve this you can use the solution proposed in "problems when used along with confirmable module", but I prefer the following solution because you don't store any request with POST or PUT.

def store_location
    if (request.path != '/users/sign_in' &&
        request.path != '/users/sign_up' &&
        request.path != '/users/password/new' &&
        request.path != '/users/password/edit' &&
        !request.xhr? && !request.post? && !request.put?
        )
      session[:previous_url] = request.fullpath
    end
  end

  def after_sign_in_path_for(resource)
    session[:previous_url] || root_path
  end

No hay comentarios:

Publicar un comentario