Hi there!
I have 3 types of users in my system, And I'd like to redirect each of him
for a differente page.
I know Devise have a helper to do this, but I'm getting some problems to
implement it
I'm started trying from the documentation
Module: Devise::Controllers::Helpers — Documentation for devise (1.1.2)
In "class ApplicationController < ActionController::Base"
def after_sign_in_path_for(resource)
if resource.is_a?(User) && resource.can_publish?
publisher_url
else
super
end
end
But I'm get a error "undefined method `can_publish?' for
#<User:0x00000102f7a578>"
Have you provided a can_publish? method in the User model?
I do not know this method, and the documentation does not show how to
implement it.
In that case I think you are in need of some basic learning. I
suggest you work through a tutorial such as railstutorial.org, which
is free to use online, that should give you a better understanding of
how to develop rails apps. Also have a look at the Rails Guides.
Let me explain it to you, but I won’t offer a solution:
def after_sign_in_path_for(resource) says if resource.is_a?(User) && resource.can_publish?
We can tell from your code that you are passing in a User object (defined in app/models/user.rb) to this method. Whatever you hadhoped this method would be doing, what it is doing is checking that object for the method can_publish? (note the question mark is part of the method name).
The error tells you that you have no def can_publish? defined in your User model (app/models/user.rb)
You must have written the after_sign_in_path_for() method at some point, or you copied it from somewhere.
Take a step back and start writing some tests. If you go to call a method on a model (User), write a unit test for that method first. Then when you’re coding the after_sign_in_path_for() method don’t call any methods on object you haven’t written a unit test for. Then learn how to do functional & integration tests too.
That’s your problem. Hope this helps.
The question is: The place when I have to do this is really the
application
controler?
The application controller is a fine place to do global (system-wide) checking of user privileges and logic around what happens to certain types of users at certain times. When you application grows larger, you can put these into a module and use include to mix these into your application controller. But that’s an advanced topic, what you’re doing is fine for now.
You should also use existing gems like RESTful authentication for authentication if you are not. Checkout http://ruby-toolbox.com/ – one of the things I like about this website is that gems get voted up & down so you see which ones are popular and can choose one that is widely adopted.