ruby - Rails 4 Devise, with after_sign_in_path_for(resource) always redirect to Show action of Model -


i'm stuck method after_sign_in_path_for of devise, thing... have 2 models users , adminusers i'm using active_admin gem

my routes.rb file, looks this:

devise_for :admin_users, activeadmin::devise.config   activeadmin.routes(self) scope "(:locale)", locale: /es|en/         devise_for :users, path_names: { sign_in: 'login', sign_out: 'logout',                                      password: 'password', confirmation: 'confirmation', unlock: 'unlock',                                      registration: 'registration', sign_up: 'sign_up' }     devise_scope :user       "login", to: "devise/sessions#new"     end 'dashboard/index' end 

ok... in application_controller.rb, tryed codes:

def after_sign_in_path_for(resource)       case resource           when adminuser             #admin_root_path              '/admin/dashboard'             puts'in admin_root_path'           when user              #dashboard_index_path              '/dashboard/index'             puts'in dashboard_index_path'         else super         end       puts 'resource of aplicationcontroller: ' + resource.class.to_s    end    def after_sign_out_path_for(resource_or_scope)     root_path   end 

as can see, have puts check in console if case works... , does, works, console: i'm trying login adminuser, admin_root_path of active_admin

started post "/admin/login?locale=es" ::1 @ 2015-04-14 12:46:51 -0400 processing activeadmin::devise::sessionscontroller#create html .... .... in admin_root_path resource of aplicationcontroller: adminuser redirected http://localhost:3000/admin/users/2?locale=es completed 302 found in 113ms (activerecord: 1.8ms) 

as can see, devise redirect me show page of current_admin_user.

and here console if try log in user:

started post "/es/users/login" ::1 @ 2015-04-14 12:51:18 -0400 processing devise::sessionscontroller#create html ... ... in dashboard_index_path resource of aplicationcontroller: user redirected http://localhost:3000/es/users/2 completed 302 found in 106ms (activerecord: 1.4ms) 

in 2 scenarios devise doing same... redirecting show action of each model, tryed put code in custom registrations_controller.rb , same... maybe i'm missing obvious, i'm not expert in devise, has idea of i'm doing wrong?

the relevant results of rake routes command

.... .... edit_admin_user_password   /admin/password/edit(.:format)                 active_admin/devise/passwords#edit  admin_root     /admin(.:format)    admin/dashboard#index .... .... admin_dashboard  /admin/dashboard(.:format)    admin/dashboard#index .... .... dashboard_index    (/:locale)/dashboard/index(.:format)   dashboard#index {:locale=>/es|en/} root    /    visitors#index 

please, try this:

def after_sign_in_path_for(resource)   if resource.class == adminuser     admin_root_path    elsif resource.class == user     dashboard_index_path    end end 

two things take care: use resourse.class think typo in question, , not @ code (because prints puts). second, after_sign_in_path must return url, , last action on code puts returns nil.

also can try:

def after_sign_in_path_for(resource)   dashboard_index_path  end 

to see after_sign_in_path_for working first time. , move forward starting point.

if doesn't work, please publish sign_in , login routes.

edit: after reading post realize case doesn't (wrong: works) work, because case works === , resource.class === adminuser return false (are diferent objects). must ask resource.class == adminuser or resource.is_a?(adminuser). more of case match here. update after_sign_in_path method.

edit 2 after reading @cjbrew comment, , these experiments @ irb:

(main) > pp = person.first => "#<person id: 1 ..." (main) > pp === person => false (main) > person === pp => true (main) > case pp (main) | when person   (main) |   'yes' (main) | end   => "yes" 

i can code must work fine :

def after_sign_in_path_for(resource)   case resource   when user     root_path   when adminuser     admin_contexts_path   end end 

because case use === when value object , case value param. in case first when clause tested with: user.===(resource) or user === resource, , second adminuser.===(resource). user , adminuser class objects, use module#=== method. important thing here aware method not commutative, class method accepts object param, , return true if param instance of class.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

node.js - How to abort query on demand using Neo4j drivers -