Different layout for sign_in action in development

I am trying to use a different / custom layout called "devise" for the sign_in action. I found this page in the development quiz, and the second example even says that you can do this for an action (in this case, sign_in action), but this does not show an example of this. Someone from the IRC told me that I could try this:

 class ApplicationController < ActionController::Base protect_from_forgery layout :layout_by_resource def layout_by_resource if devise_controller? && resource_name == :user && action_name == 'sign_in' "devise" else "application" end end end 

But it doesn't seem to work, as it still loads the default application layout. I would appreciate any help.

+79
ruby-on-rails layout devise
Feb 13 2018-11-11T00:
source share
8 answers

I understood this, but I will keep this question here in case other people are interested.

It was a stupid mistake. The fact sign_in is the path, not the action. If you look at the corresponding source , I see that the required action is new , i.e. Create a new development session. Change my code above:

 if devise_controller? && resource_name == :user && action_name == 'new' 

It works great.

Hope someone helps there.

+43
Feb 13 '11 at 2:45
source share
β€” -

Another way to apply a custom layout for an action is as follows.

According to How to Create Custom Layouts , you can also set the layout for specific Devise controllers using the callback in config / environment.rb (rails 2) or config / application.rb (rails 3). This needs to be done in the to_prepare callback. because it is executed once during the production process and before each request in development. "

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

Typically, the distinction between layouts is made between pages behind inputs and pages that do not require authentication, so this approach works most of the time. But I also experimented using the action_name to set the layout for a specific action, and it worked like a charm:

 config.to_prepare do Devise::SessionsController.layout proc{ |controller| action_name == 'new' ? "devise" : "application" } end 

I think this is better and built into the way to change the layout based on the development of the controller / action, instead of creating an assistant in the ApplicationController.

+90
Aug 21 '11 at 17:08
source share

I just created the application /views/layouts/devise/sessions.html.erb and placed my layout there.

+60
Jun 17 2018-12-12T00:
source share

Here is how I did it. I need a different layout if the user was to log in, but a different layout if the user was to edit my profile.

I am using Rails 4.1.1

In the application controller, add the following:

 class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception before_action :configure_permitted_parameters, if: :devise_controller? layout :layout_by_resource # Define the permitted parameters for Devise. protected def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:firstname, :lastname, :email, :password, :password_confirmation)} devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:avatar, :firstname, :lastname, :email, :password, :password_confirmation, :current_password) } end def layout_by_resource if devise_controller? and user_signed_in? 'dashboard' else 'application' end end end 
+8
May 12 '14 at
source share

The easiest solution is to simply create a layout called devise.html.haml in the app / views / layouts folder. and Rails magic takes care of the rest.

 app/views/layouts/devise.html.haml 
+7
Jun 02 '15 at 20:39
source share

Surprised not to see this answer anywhere, but you can also do this:

In routes.rb, change your project configuration to look something like this:

  devise_for :users, controllers: { sessions: 'sessions' } 

Then in app / controller / sessions_controller.rb

 class SessionsController < Devise::SessionsController layout 'devise', only: [:new] end 

This is especially useful if you need to perform additional logical overrides in any of the Devise controllers.

+5
Dec 16 '14 at 20:47
source share

You just don’t know, you can also use rake routes to see the routes in your rails application along with the action / controller file to which they are attached.

  new_user_registration GET /accounts/sign_up(.:format) {:action=>"new", :controller=>"devise/registrations"} edit_user_registration GET /accounts/edit(.:format) {:action=>"edit", :controller=>"devise/registrations"} PUT /accounts(.:format) {:action=>"update", :controller=>"devise/registrations"} DELETE /accounts(.:format) {:action=>"destroy", :controller=>"devise/registrations"} 
+1
Feb 13 2018-11-11T00:
source share

Here is one liner for those who want everyone to design actions for using the new layout:

 class ApplicationController < ActionController::Base protect_from_forgery layout Proc.new { |controller| controller.devise_controller? ? 'devise' : 'application' } end 
0
Aug 12 2018-12-12T00:
source share