Thursday, 20 August 2015

How To: Create custom layouts for devise:


1, Define layouts in the ApplicationController
2, Define layouts in config (config/environment.rb for rails 2, config/application.rb in rails 3)

Define layouts in the ApplicationController:

class ApplicationController < ActionController::Base
  layout :check_layout

  protected

  def check_layout
    if devise_controller? && resource_name == :user && action_name == "new"
      "layout_for_devise"
    else
      "application"
    end
  end
end

devise_controller? --> Determine weather it's devise controller or not
resource_name ---> Dealing with roles
action_name --> To determine specific action

2, Define in config:

Below code need to write in application.rb:

#to set layout for specific controller
config.to_prepare do
  Devise::SessionsController.layout "layout_for_sessions_controller"
  Devise::RegistrationsController.layout proc { |controller| user_signed_in? ? "application" : "devise" }
end

#to set layout for devise mailer
config.to_prepare do
  Devise::Mailer.layout "email_layout" #email_layout.erb
end

If u want to write these configurations in devise.rb:
# append to end of config/initializers/devise.rb
Rails.application.config.to_prepare do
  Devise::SessionsController.layout "layout_for_sessions_controller"
  Devise::RegistrationsController.layout proc { |controller| user_signed_in? ? "application" : "layout_for_registration_controller" }
end


that's it..

To generate devise views:
rails generate devise:views

To destroy generate devise views:
rails destroy devise:views

if you want to destroy an especific set of views use:
rails destroy devise:views model
instead of model use the name of the model you want to destroy, for example user, or admin whichever your model is.

No comments:

Post a Comment