[英]Change layout for Devise controllers
I am using gem devise
. 我正在使用gem devise
。 Devise extends application controller and adds user managment to rails application. Devise扩展了应用程序控制器,并向Rails应用程序添加了用户管理。
When I look inside the gem I can see following line 当我查看宝石内部时,可以看到以下行
class Devise::SessionsController < ApplicationController
I am trying to change this since I want Devise controller to inherit from my custom controller named AdminController
. 我要更改此设置,因为我希望Devise控制器继承自名为AdminController
自定义控制器。 Reason for this is I have whole web application finished and I do not want admin part of the page to use my application layout, css, js ... 原因是我已经完成了整个Web应用程序,并且我不希望页面的admin部分使用我的应用程序布局,css,js ...
How can I dynamically change base class of controller? 如何动态更改控制器的基类? Or dynamically tell controller to use admin.html.erb
layout instead of application.html.erb
layout. 或者动态告诉控制器使用admin.html.erb
布局而不是application.html.erb
布局。
When I say "dynamicly" I mean monkey patch it, thank you. 当我说“动态”时,我的意思是猴子打补丁,谢谢。
Devise is a rails engine . Devise是Rails引擎 。 I think that the best way to make a admin section of you site is to make a rails engine. 我认为,使网站的管理部分最好的方法是制作Rails引擎。 Or better still use rails_admin or activeadmin . 或者最好还是使用rails_admin或activeadmin 。 They are both rails engines There is a railscast about rails engines 他们都是铁路引擎。关于铁路引擎的铁轨
I don't know the inner works of you app, but if you add 我不知道您应用的内部功能,但是如果您添加
layout "admin"
to your AdminController and add a custom admin layout to the view/layouts folder with 到您的AdminController并将自定义管理布局添加到view / layouts文件夹,其中
<%= stylesheet_link_tag 'admin' %>
<%= javascript_include_tag "admin"%>
the AdminController views will use the admin stylesheet and javascript AdminController视图将使用管理样式表和javascript
This solved my problem, if namespace of controller is Devise
use admin layout. 这解决了我的问题,如果控制器的名称空间是Devise
使用管理员布局。
class ApplicationController < ActionController::Base
protect_from_forgery
layout :determine_layout
def determine_layout
module_name = self.class.to_s.split("::").first
return (module_name.eql?("Devise") ? "admin" : "application")
end
end
If you just need to change the layout, I think you should be able to do it by re-opening the controller class. 如果只需要更改布局,我认为您应该可以通过重新打开控制器类来做到这一点。 At the bottom of your initializers/devise.rb
(underneath the config section at the top level, you could write: 在initializers/devise.rb
的底部(在顶层的config部分下面,您可以编写:
Devise::SessionsController.layout :admin
I've not tried this, but in theory it should work since layout is just a class method on ActionController.base. 我没有尝试过,但是从理论上讲应该可以使用,因为布局只是ActionController.base上的类方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.