Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise redirect after sign up

Devise is giving me a pretty hard time. For now, everything else seems to be working, except the sign up redirect. I want devise to redirect to my town controller at index action, upon sign up or login (login actually works).

I've tried overriding RegistrationsController and i've tried adding an applicationsController function like :

  def after_sign_in_path_for(resource_or_scope)
    if resource_or_scope.is_a?(User)
      town_path
    else
      super
    end
  end

Still, i'm getting the same error :

NoMethodError in User/townController#index

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.*

Seriously, i cannot find a way to do it. Any ideas please ? :)

EDIT : MY ROUTES

      new_user_session GET    /users/sign_in(.:format)                       {:action=>"new", :controller=>"devise/sessions"}
          user_session POST   /users/sign_in(.:format)                       {:action=>"create", :controller=>"devise/sessions"}
  destroy_user_session GET    /users/sign_out(.:format)                      {:action=>"destroy", :controller=>"devise/sessions"}
         user_password POST   /users/password(.:format)                      {:action=>"create", :controller=>"devise/passwords"}
     new_user_password GET    /users/password/new(.:format)                  {:action=>"new", :controller=>"devise/passwords"}
    edit_user_password GET    /users/password/edit(.:format)                 {:action=>"edit", :controller=>"devise/passwords"}
                       PUT    /users/password(.:format)                      {:action=>"update", :controller=>"devise/passwords"}
     user_registration POST   /users(.:format)                               {:action=>"create", :controller=>"devise/registrations"}
 new_user_registration GET    /users/sign_up(.:format)                       {:action=>"new", :controller=>"devise/registrations"}
edit_user_registration GET    /users/edit(.:format)                          {:action=>"edit", :controller=>"devise/registrations"}
                       PUT    /users(.:format)                               {:action=>"update", :controller=>"devise/registrations"}
                       DELETE /users(.:format)                               {:action=>"destroy", :controller=>"devise/registrations"}
                  root        /(.:format)                                    {:action=>"index", :controller=>"home"}
             user_root        /user(.:format)                                {:action=>"index", :controller=>"user/town"}
                  home        /home(.:format)                                {:action=>"index", :controller=>"home"}
                  town        /town(.:format)                                {:action=>"index", :controller=>"town"}
                 inbox        /messages(.:format)                            {:action=>"index", :controller=>"messages"}
                 inbox        /messages/inbox(.:format)                      {:action=>"inbox", :controller=>"messages"}

Routes.rb :

  devise_for :users

  root :to => "home#index"

  namespace :user do
    root :to => "town#index"
  end  

  scope :path => '/home', :controller => :home do
    match '/' => :index, :as => 'home'
  end

  scope :path => '/town', :controller => :town do
    match '/' => :index, :as => 'town'
  end
......
like image 489
Spyros Avatar asked Feb 14 '11 00:02

Spyros


3 Answers

Im using Devise 1.3.4 with Ruby On Rails 3.0.7. After checking out the internet for a solution, what i did was simply pasting the following code

*first)*To redirect after a succesful sign up to the welcome page,place in /config/routes.rb the following (note, replace :user with the argument you provided to devise_for):

namespace :user do
root :to => "welcome#index"
end

*second)*To redirect after a sign out (to the welcome page also), place in the /app/controllers/application_controller.rb this method:

private
# Overwriting the sign_out redirect path method
def after_sign_out_path_for(resource_or_scope)
welcome_index_path
end

This worked for me, i hope it does for all of you.

like image 123
Kike Avatar answered Oct 17 '22 18:10

Kike


An answer to this question that worked perfectly for me is explained on the Devise Wiki here:
How To: Redirect to a specific page on successful sign up (registration)

like image 40
cailinanne Avatar answered Oct 17 '22 18:10

cailinanne


This is how I got this working.

# In your routes.rb
match 'dashboard' => 'user_dashboard#index', :as => 'user_root'

Then make sure you don't have a before_filter :authenticate_user! set on your home#index controller.

like image 11
Tony Avatar answered Oct 17 '22 18:10

Tony