Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3, Devise: Using different layout for signup, login page

I want to render a different layout for signup and login pages.

There's s similar thread that deals with this problem, but it's not exactly the same.

I need to be able to render different layout just for signup and login pages only, not all the other actions in a controller.

under users/registrations_controller.rb

class Users::RegistrationsController < Devise::SessionsController
  def new
    render :layout => "auth"
  end
end

My routes

MasterCard::Application.routes.draw do
  devise_for :users, :controllers => { :registrations => "users/registrations" }, :path => "users", :path_names => { :sign_in => 'login', :sign_out => 'logout' }

  devise_scope :user do
    get "login", :to => "users/sessions#new"
    # post "logout", :to => "users/sessions"
  end

  root :to => 'pages#home'
  match '/about' => 'pages#about'
end

This is the error I get when i go to sign up page.

undefined methoderrors' for nil:NilClass`

like image 266
Jason Kim Avatar asked Jan 19 '26 06:01

Jason Kim


1 Answers

First, do you mean to subclass Devise::RegistrationsController and not Devise::SessionsController?

Overriding devise controller actions seems a little hairy. You can avoid this in your case by just overriding the default layout that RegistrationsController uses:

class Users::RegistrationsController < Devise::RegistrationsController
  layout "auth"
end

As for why you're getting that particular error:

You're redefining the new action in what I'm assuming should be Devise::RegistationsController which has the following definition:

def new
  resource = build_resource({})
  respond_with resource
end

It sets a resource which is then referenced in the devise helper method devise_error_messages!:

def devise_error_messages!
  return "" if resource.errors.empty?
  ...
end

which is used in the default devise "sign_up" template, users/registrations/new.html.erb:

...
<%= form_for(resource, :as => resource_name, ...) do |f| %>
  <%= devise_error_messages! %>
...

You see that error because you didn't define resource.

like image 127
cdesrosiers Avatar answered Jan 21 '26 00:01

cdesrosiers