Ruby-on-rails 在命名空间内设计

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4500932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:57:15  来源:igfitidea点击:

Devise within namespace

ruby-on-railsnamespacesruby-on-rails-pluginsdevise

提问by Harm de Wit

I'm trying to split my rails project in a front-end for regular users and a back-end for admins. Therefore i have created a namespace 'admin' so that i can easily control admin specific controller methods/layouts/authentication in the map admin.

我正在尝试将我的 rails 项目拆分为一个前端供普通用户使用,一个后端供管理员使用。因此,我创建了一个命名空间“admin”,以便我可以在地图管理员中轻松控制管理员特定的控制器方法/布局/身份验证。

I'm using Devise to register/authenticate my admins only. Because it is only used for admins only i'm trying to move Devise to the admin namespace.

我仅使用 Devise 来注册/验证我的管理员。因为它仅用于管理员,所以我正在尝试将设计移动到管理员命名空间。

I could not find exactly what i was looking for in the documentationof Devise but i tried something like this in routes.rb:

我在Devise的文档中找不到我正在寻找的内容,但我在 routes.rb 中尝试了类似的东西:

namespace 'admin'do 
  devise_for :admins
end

I also tried to make a custom Devise::Sessions controller but that too didn't seem to work out.

我还尝试制作一个自定义的 Devise::Sessions 控制器,但这似乎也没有奏效。

Does anyone know how to do this? Should i just use the regular routes for devise with a custom(admin) layout?

有谁知道如何做到这一点?我应该只使用常规路线进行自定义(管理)布局设计吗?

回答by iain

Simply "moving" Devise to the admin namespace is wrong. Devise uses controllers like Devise::SessionsControllerand that cannot be "moved".

简单地“移动”设计到 admin 命名空间是错误的。设计使用Devise::SessionsController不能“移动”的控制器。

I usually create my own controllers and inherit them from Devise:

我通常创建自己的控制器并从 Devise 继承它们:

class Admin::SessionsController < ::Devise::SessionsController
  layout "admin"
  # the rest is inherited, so it should work
end

And configure this in config/routes.rb:

并在config/routes.rb

devise_for :admins, :controllers => { :sessions => "admin/sessions" }

Oryou could change the layout only, by making the layout a bit more complex:

或者,您可以通过使布局更复杂一点来仅更改布局:

class ApplicationController < ActionController::Base

  layout :layout

  private

  def layout
    if devise_controller? && devise_mapping.name == :admin
      "admin"
    else
      "application"
    end
  end

end

回答by Hyman Chu

How about just moving the devise_formethod into a scope:

devise_for方法移动到作用域中如何:

scope '/admin' do
  devise_for :admins
end

With namespace, the controllers will try to look for an Admin::SessionControllerthat wont exist. With scope it doesn't, so that should work.

使用命名空间,控制器将尝试查找不Admin::SessionController存在的。对于范围,它没有,所以应该工作。

回答by Ronak Jain

How about specifying devise the path to take, place this outside your namespace.

如何指定设计要采用的路径,将其放置在您的命名空间之外。

devise_for :users, path: 'admins'

This will generate the following routes

这将生成以下路线

new_user_session          GET      /admins/sign_in(.:format)          devise/sessions#new
user_session              POST     /admins/sign_in(.:format)          devise/sessions#create
destroy_user_session      DELETE   /admins/sign_out(.:format)         devise/sessions#destroy
user_password             POST     /admins/password(.:format)         passwords#create
new_user_password         GET      /admins/password/new(.:format)     passwords#new
edit_user_password        GET      /admins/password/edit(.:format)    passwords#edit
                          PUT      /admins/password(.:format)         passwords#update
cancel_user_registration  GET      /admins/cancel(.:format)           registrations#cancel
user_registration         POST     /admins(.:format)                  registrations#create
new_user_registration     GET      /admins/sign_up(.:format)          registrations#new
edit_user_registration    GET      /admins/edit(.:format)             registrations#edit
                          PUT      /admins(.:format)                  registrations#updat
                          DELETE   /admins(.:format)                  registrations#destroy

You don't have to change anything in that case, if this is what you are looking for.

在这种情况下,您不必更改任何内容,如果这是您要查找的内容。

Happy Coding :)

快乐编码:)

回答by Saldan

Both Hyman Chuand iainsolutions should solve the problem plus generating your views in order to customize the layout of the login form.

无论Hyman楚伊恩解决方案要解决这个问题再加以定制登录表单的布局产生你的意见。

So in your config/routes.rbyou should have

所以在你的config/routes.rb你应该有

scope '/subfolder' do
   devise_for :admins, :controllers => { :sessions => "subfolder/sessions" }
end

namespace :subfolder do
  match '/', :to => 'subcontroller#action'
end

Remember di create your own controllers for sessions as you are already doing. Probably you will need to generate your views, too by using rails generate devise:views

请记住,正如您已经在做的那样,为会话创建自己的控制器。可能您也需要通过使用来生成您的视图rails generate devise:views

Check thisfor any doubt on devise tasks.

检查此是否对设计任务有任何疑问。

回答by Guabi Delabatte

If you want to put your devise views in views/admin/admins/ and your controllers in controllers/admin/admins/:

如果你想把你的设计视图放在 views/admin/admins/ 中,你的控制器放在 controller/admin/admins/ 中:

your sessions_controller.rb in controllers/admin/admins:

你的sessions_controller.rb 在controllers/admin/admins 中:

class Admin::Admins::SessionsController < ::Devise::SessionsController
  layout "admin/connection"
end

routes.rb :

路线.rb :

namespace :admin do |admin|
    devise_for :admins, :controllers => { :sessions => "admin/admins/sessions" }
end

Generating devise views :

生成设计视图:

rails g devise:views admin/admins

回答by Harm de Wit

In addition to the first solution of the answer of iain i had to generate views of devise or else it throws a template missing exception.

除了 iain 答案的第一个解决方案之外,我还必须生成设计视图,否则会引发模板丢失异常。

generate views with

生成视图

rails g devise_views

The views will be located in views>devise. Move the created map 'sessions' to the map views>admin

视图将位于视图>设计中。将创建的地图“会话”移动到地图视图>管理