I Can not redirect after login with Devise

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

http://rubydoc.info/gems/devise/1.1.2/Devise/Controllers/Helpers

In “class ApplicationController < ActionController::Base”

def after_sign_in_path_for(resource)
if resource.is_a?(User) && resource.can_publish?
publisher_url
else
super
endend

But I’m get a error “undefined method `can_publish?’ for
#User:0x00000102f7a578

I also tryed another examples who I found through google. But when I
implement, nothing happens. I’m a bit lost for doing this.

I’d like to put conditionals in controller to get a type from each user
and
redirect them for an especific page. For example

if current_user.type == “Admin”
redirect_to “http://example.com
end

The question is: The place when I have to do this is really the
application
controler?
And someone know any example who works correctly?

thanks for help!


Fernando A.

On 8 June 2011 16:22, Fernando A. [email protected]
wrote:

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?

Colin

On 8 June 2011 16:44, Fernando A. [email protected]
wrote:

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.

Colin

Hi!

Thanks by the tips!

Actually I’m learning Rails, and I already see this tutorial, is very
good.

I’m already do too Rails for zoombies, Rails best pratices and a lot
other
tutorials.

I’m doying my first web app, just to practice what I learned.

but none of them learned about this method, and is the first time I
am
working with devise.

I’m already work with anothers languages too.

So, if anyone can help me with that, would be great.

Thanks!

On Wed, Jun 8, 2011 at 1:34 PM, Colin L. [email protected]
wrote:

Colin

I have 3 types of users in my system, And I’d like to redirect each
def after_sign_in_path_for(resource)
Have you provided a can_publish? method in the User model?
redirect_to “http://example.com
Fernando A.
http://groups.google.com/group/rubyonrails-talk?hl=en.
http://groups.google.com/group/rubyonrails-talk?hl=en.



Fernando A.

[iOSDeveloper] - ObjectiveC
[WebDesigner] - CSS3&HTML5
[WebDeveloper] - RubyOnRails
*--------------------------------------------------------------
*
[portfolio] http://www.fernandoaureliano.com -
[blog]http://www.fernandoaureliano.com/

Fernando,

The problem is right in your code.

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 had hoped
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.

Jason

This helps a lot.

Thanks!

On Wed, Jun 8, 2011 at 3:51 PM, Jason Fleetwood-Boldt
[email protected]wrote:

in app/models/user.rb) to this method. Whatever you had* *hoped this
on a model (User), write a unit test for that method first. Then when you’re

authentication if you are not. Checkout http://ruby-toolbox.com/ – one of
Thanks by the tips!

Thanks!

On Wed, Jun 8, 2011 at 12:30 PM, Colin L. [email protected]
to

end
implement, nothing happens. I’m a bit lost for doing this.
controler?
“Ruby on Rails: Talk” group.
Groups

You received this message because you are subscribed to the Google
You received this message because you are subscribed to the Google G.


You received this message because you are subscribed to the Google G.
“Ruby on Rails: Talk” group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Fernando A.

[iOSDeveloper] - ObjectiveC
[WebDesigner] - CSS3&HTML5
[WebDeveloper] - RubyOnRails
*--------------------------------------------------------------
*
[portfolio] http://www.fernandoaureliano.com -
[blog]http://www.fernandoaureliano.com/

Get it to work! =D

private
def stored_location_for(resource_or_scope)
nil
end
def after_sign_in_path_for(resource_or_scope)
http://example.com
end

Thanks by the tips! :wink:

On Wed, Jun 8, 2011 at 3:55 PM, Fernando A. <
[email protected]> wrote:

The problem is right in your code.

integration tests too.
The application controller is a fine place to do global (system-wide)

good.
working with devise.

suggest you work through a tutorial such as railstutorial.org, which

I’m started trying from the documentation

user

[email protected]

To post to this group, send email to
Fernando A.
“Ruby on Rails: Talk” group.
“Ruby on Rails: Talk” group.
Fernando A.

You received this message because you are subscribed to the Google G.


Fernando A.

[iOSDeveloper] - ObjectiveC
[WebDesigner] - CSS3&HTML5
[WebDeveloper] - RubyOnRails
*--------------------------------------------------------------
*
[portfolio] http://www.fernandoaureliano.com -
[blog]http://www.fernandoaureliano.com/

I do not know this method, and the documentation does not show how to
implement it.

=(

On Wed, Jun 8, 2011 at 12:30 PM, Colin L. [email protected]
wrote:

In “class ApplicationController < ActionController::Base”
#User:0x00000102f7a578"
if current_user.type == “Admin”


Fernando A.

[iOSDeveloper] - ObjectiveC
[WebDesigner] - CSS3&HTML5
[WebDeveloper] - RubyOnRails
*--------------------------------------------------------------
*
[portfolio] http://www.fernandoaureliano.com -
[blog]http://www.fernandoaureliano.com/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs