将Devise与多个名称空间用于同一模型 [英] Using Devise with multiple namespaces for the same Model

查看:175
本文介绍了将Devise与多个名称空间用于同一模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Devise与两个命名空间:API命名空间和默认命名空间
,但是当用户尝试登录时,这两个命名空间似乎是冲突的。
含义,引用的任何命名空间Devise首先结束
作为最终重定向的地方。例如:如果我尝试在默认命名空间下创建一个新的会话
,那么该用户会话路径
将失败,然后尝试在API / v1会话路径上创建会话。



我如何使两者独立行事?



他们都引用了一个User对象。 user_sessions控制器
的默认命名空间是'user_sessions'。 API / V1命名空间的user_sessions
控制器为'/ api / v1 / user_sessions'

  ---- ROUTES.RB ------- 
MySite :: Application.routes.draw do
命名空间:api do
命名空间:v1 do
devise_for:users,:controllers = > {:sessions => api / v1 /
user_sessions,:registrationrations =>users}
...
end
end
devise_for:users, controllers => {:sessions =>user_sessions,:registrations =>users} do
post'users / sign_in'=> 'user_sessions#create',:as => :user_session
get'users / sign_in'=> 'user_sessions#new',:as => :new_user_session
get'users / sign_up'=> 'user_sessions#new',:as => :new_user_session
match'users / sign_out'=> 'user_sessions#destroy',:as => :destroy_user_session
< .....>
end

----- DEFAULT NAMESPACE USER_SESSIONS_CONTROLLER -----

  class UserSessionsController< Devise :: SessionsController 
....
end
----

API NAMESPACE USER_SESSIONS_CONTROLLER ----(这是我下面标记的我的自定义Devise基本控制器)

  class Api: :V1 :: UserSessionsController< Api :: V1 :: DeviseBaseController 
...
end

- - API NAMESPACE CUSTOM DEVISE BASE CONTROLLER ----

  class Api :: V1 :: DeviseBaseController< Devise :: SessionsController 
respond_to:json
end


解决方案

我发现这是不可能的,Devise,你必须有一个登录/注销源。



清洁解决方案:为您的API和Web命名空间创建指向相同Devise控制器代码(例如/ user / sessions)的路由。在那里,调用partials来获得适当的响应(JSON,HTML)。这些部分可以坐在每个命名空间的视图目录中,保持清洁。


I want to use Devise with two namespaces: an API namespace, and the default namespace, but the two seem to be conflicting when a user tries to sign in. Meaning, whichever namespace that references Devise first ends up being the final redirection place. Ex: If I try to create a new session under the default namespace it will fail on that user session path, and then attempt to create the session on the API/v1 sessions path.

How do I make the two act independently?

They are both referencing a User object. The user_sessions controller for the default namespace is 'user_sessions'. The user_sessions controller for the API/V1 namespace is '/api/v1/user_sessions'

    ---- ROUTES.RB -------
    MySite::Application.routes.draw do
    namespace :api do
   namespace :v1 do
     devise_for :users,:controllers => { :sessions => "api/v1/
user_sessions",:registrations=>"users" }
     ......
   end
 end
 devise_for :users,:controllers => { :sessions =>"user_sessions",:registrations=>"users" } do
    post 'users/sign_in' => 'user_sessions#create', :as => :user_session
    get 'users/sign_in' => 'user_sessions#new', :as => :new_user_session
    get 'users/sign_up' => 'user_sessions#new', :as => :new_user_session
    match 'users/sign_out' => 'user_sessions#destroy', :as => :destroy_user_session
    <.....>
 end

----- DEFAULT NAMESPACE USER_SESSIONS_CONTROLLER -----

class UserSessionsController < Devise::SessionsController
....
end
---- 

API NAMESPACE USER_SESSIONS_CONTROLLER ---- (this goes to my custom Devise base controller marked below)

class Api::V1::UserSessionsController < Api::V1::DeviseBaseController
...
end

---- API NAMESPACE CUSTOM DEVISE BASE CONTROLLER ----

class Api::V1::DeviseBaseController < Devise::SessionsController
       respond_to :json
end

解决方案

I found out that this isn't possible with Devise, you have to have one sign-in/sign out source.

Clean solution: Create routes for both your API and Web namespaces that point back to the same Devise controller code (say, /user/sessions). In there, call partials for the appropriate response (JSON, HTML). Those partials can sit in the view directories for each namespace, keeping things clean.

这篇关于将Devise与多个名称空间用于同一模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆