Changing devise default layouts

我们两清 提交于 2020-07-04 10:45:28

问题


I'm trying to get devise to display a different layout template from the defaults for certain aspects.

I have this working when the user goes to the login page, but I need to display a different layout for sign up, forgotten password, and reset.

This is the current code in my application controller:

layout :layout

  private
  def layout
    # only turn it off for login pages:
    is_a?(Devise::SessionsController) ? "login" : "application"
    # or turn layout off for every devise controller:
    #devise_controller? && "application"
  end

回答1:


If you name your alternate layout devise.html.erb, then the gem's controllers will naturally use it, without needing to be asked. Saves some code.




回答2:


Add this lines of code to your application.rb:

config.to_prepare do
    Devise::SessionsController.layout "your_layout_name"
    Devise::RegistrationsController.layout "your_layout_name"
    Devise::ConfirmationsController.layout "your_layout_name"
    Devise::UnlocksController.layout "your_layout_name"
    Devise::PasswordsController.layout "your_layout_name"
end

If you want the same layout for all Devise views, except for when the user is editing its data, you could have something like this:

config.to_prepare do
  Devise::SessionsController.layout "your_layout_name"
  Devise::RegistrationsController.layout proc{ |controller| user_signed_in? ? "application" : "your_layout_name" }
  Devise::ConfirmationsController.layout "your_layout_name"
  Devise::UnlocksController.layout "your_layout_name"            
  Devise::PasswordsController.layout "your_layout_name"        
end

For more information you can read this article




回答3:


You don't need to handle the layouts by your self, just do:

rails generate devise:views

Then, look at devise folder at views folder, you will see all the forms you need to customize



来源:https://stackoverflow.com/questions/11082213/changing-devise-default-layouts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!