Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
543 views
in Technique[技术] by (71.8m points)

devise - devise和devise_token_auth在路由上不起作用(devise and devise_token_auth don't work togethor with routes)

I have successfully implemented devise_token_auth and i am trying to implement regular devise.

(我已经成功实现了devise_token_auth,并且我正在尝试实现常规的devise。)

For devise_token_auth, I have ApiController which inherits from ApplicationController .

(对于devise_token_auth,我有ApiController ,它继承自ApplicationController 。)

Here is the routes file:

(这是路由文件:)

Rails.application.routes.draw do
    defaults format: :json do
      namespace :api do
        resources :appointments, only: [:index]
        # get 'appointments/index'
      end
    end
    scope module: :web do
        root to: 'pages#home'
    end
    mount_devise_token_auth_for 'User', at: 'api'
end

and here's one of the controllers that successfully inherits from ApiController :

(这是成功继承自ApiController的控制器ApiController :)

class Api::AppointmentsController < ApiController
  before_action :authenticate_user!
  def index
    @appointments = Appointment.all
  end
end

I am correctly returned json informing me to login:

(我正确返回了json,通知我登录:)

{
    "errors": [
        "You need to sign in or sign up before continuing."
    ]
}

Here is the pages controller:

(这是页面控制器:)

class Web::PagesController < ApplicationController
  before_action :authenticate_user!
  def home

  end
end

If I comment out the before_action :authenticate_user!

(如果我将before_action :authenticate_user!)

in the PagesController, I get to the right page in the page.

(在PagesController中,我转到页面的正确页面。)

However, if I add the before_action :authenticate_user!

(但是,如果我添加before_action :authenticate_user!)

, instead of being redirected to http://localhost:3000/web/sign_in , I am directed to http://localhost:3000/api/sign_in , which is json instead of a view.

(,而不是重定向到http://localhost:3000/web/sign_in ,而是定向到http://localhost:3000/api/sign_in ,它是json而不是视图。)

I have tried adding devise_for :users, path: 'web' to the routes, but it doesn't work and the server won't start.

(我尝试将devise_for :users, path: 'web'到路由中,但是它不起作用,服务器也无法启动。)

It gives me the message

(它给我信息)

You may have defined two routes with the same name using the `:as` option, or you may be overriding a route already defined by a resource with the same naming. For the latter, you can restrict the routes created with `resources` as explained here: 

Any ideas?

(有任何想法吗?)

  ask by Philip7899 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...