简体   繁体   中英

Ruby on Rails Devise login have to sign in twice

I am using Device for my authentication. Using rails 4.1. I have it setup as follows:

The root of my project is defined as root 'projects#index' When first hitting my server, it goes to the url /users/sign_in . When I type in a username and password and click sign in, redirects me to the same sign in page with a bubble that says successfully logged in at the top. When I click sign in again, it goes to the routes error:

No route matches [PATCH] "/users/sign_in"

routes.rb

Rails.application.routes.draw do
    devise_for :users

    resources :projects do
      member do
        post 'base_images'
        get 'base_images'
      end
    end
    root 'projects#index'
end

ApplicationController.rb

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  before_action :authenticate_user!

end

user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable
end

If you then change the url manually to /projects it works as expected.

How come after sign in, it won't redirect to the root of my rails application? Any help would be much appreciated.

You can specify a path to be redirected to after a successful login, just add this to your Application Controller:

def after_sign_in_path_for(resource)
  your_custom_path
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM