Ruby-on-rails Rails & Devise:如何在没有布局的情况下呈现登录页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4412018/
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
Rails & Devise: How to render login page without a layout?
提问by neezer
回答by iain
You can subclass the controller and configure the router to use that:
您可以子类化控制器并配置路由器以使用它:
class SessionsController < Devise::SessionsController
layout false
end
And in config/routes.rb:
并在config/routes.rb:
devise_for :users, :controllers => { :sessions => "sessions" }
You need to move the session views to this controller too.
您也需要将会话视图移动到此控制器。
ORmake a method in app/controllers/application_controller.rb:
或在app/controllers/application_controller.rb以下位置创建一个方法:
class ApplicationController < ActionController::Base
layout :layout
private
def layout
# only turn it off for login pages:
is_a?(Devise::SessionsController) ? false : "application"
# or turn layout off for every devise controller:
devise_controller? && "application"
end
end
回答by Paul Raupach
You can also create a sessions.html.erb file in app/views/layouts/devise. That layout will then be used for just the sign in screen.
您还可以在 app/views/layouts/devise 中创建一个 session.html.erb 文件。然后该布局将仅用于登录屏幕。
回答by msroot
By using the devise_controller? helper you can determine when a Devise controller is active and respond accordingly. To have Devise use a separate layout to the rest of your application, you could do something like this:
通过使用 devise_controller?helper 您可以确定设计控制器何时处于活动状态并做出相应的响应。要让 Devise 对应用程序的其余部分使用单独的布局,您可以执行以下操作:
class ApplicationController < ActionController::Base
layout :layout_by_resource
protected
def layout_by_resource
if devise_controller?
"devise"
else
"application"
end
end
end
create a devise.html.erb file in your views/layouts
在您的视图/布局中创建一个 devise.html.erb 文件
So if its a device controller will render the devise layout else the application layout
因此,如果它的设备控制器将呈现设计布局,否则应用程序布局
from: https://github.com/plataformatec/devise/wiki/How-To:-Create-custom-layouts
来自:https: //github.com/plataformatec/devise/wiki/How-To: -Create-custom-layouts

