# I Can not redirect after login with Devise

**URL:** <https://www.ruby-forum.com/t/i-can-not-redirect-after-login-with-devise/208031>\
**Category:** Rails\
**Created:** [June 8, 2011, 5:23pm UTC](https://www.ruby-forum.com/t/i-can-not-redirect-after-login-with-devise/208031 "2011-06-08T17:23:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [June 8, 2011, 5:23pm UTC](https://www.ruby-forum.com/t/i-can-not-redirect-after-login-with-devise/208031/1 "2011-06-08T17:23:44Z")

</div>

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](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](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._

---

<div class="post-metadata">

**Author:** ![fernandoaleman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/eb8c5e/32.png) [@fernandoaleman](https://www.ruby-forum.com/u/fernandoaleman)\
**Post date:** [June 8, 2011, 5:54pm UTC](https://www.ruby-forum.com/t/i-can-not-redirect-after-login-with-devise/208031/2 "2011-06-08T17:54:01Z")

</div>

On 8 June 2011 16:22, Fernando A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![fernandoaleman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/eb8c5e/32.png) [@fernandoaleman](https://www.ruby-forum.com/u/fernandoaleman)\
**Post date:** [June 8, 2011, 7:48pm UTC](https://www.ruby-forum.com/t/i-can-not-redirect-after-login-with-devise/208031/3 "2011-06-08T19:48:58Z")

</div>

On 8 June 2011 16:44, Fernando A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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](http://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

---

<div class="post-metadata">

**Author:** ![fernandoaleman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/eb8c5e/32.png) [@fernandoaleman](https://www.ruby-forum.com/u/fernandoaleman)\
**Post date:** [June 8, 2011, 8:30pm UTC](https://www.ruby-forum.com/t/i-can-not-redirect-after-login-with-devise/208031/4 "2011-06-08T20:30:21Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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](http://example.com)”  
> > > > Fernando A.  
> > > > [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).  
> > > > [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](http://www.fernandoaureliano.com) -  
[blog][http://www.fernandoaureliano.com/](http://www.fernandoaureliano.com/)

- [personal blog] [http://www.obsessivocompulsivo.com/](http://www.obsessivocompulsivo.com/) -  
[twitter][http://twitter.com/Pailoro](http://twitter.com/Pailoro)

---

<div class="post-metadata">

**Author:** ![fernandoaleman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/eb8c5e/32.png) [@fernandoaleman](https://www.ruby-forum.com/u/fernandoaleman)\
**Post date:** [June 8, 2011, 8:52pm UTC](https://www.ruby-forum.com/t/i-can-not-redirect-after-login-with-devise/208031/5 "2011-06-08T20:52:38Z")

</div>

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/](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

---

<div class="post-metadata">

**Author:** ![fernandoaleman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/eb8c5e/32.png) [@fernandoaleman](https://www.ruby-forum.com/u/fernandoaleman)\
**Post date:** [June 8, 2011, 8:57pm UTC](https://www.ruby-forum.com/t/i-can-not-redirect-after-login-with-devise/208031/6 "2011-06-08T20:57:34Z")

</div>

This helps a lot.

Thanks!

On Wed, Jun 8, 2011 at 3:51 PM, Jason Fleetwood-Boldt  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)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/](http://ruby-toolbox.com/) – one of  
> Thanks by the tips!
> 
> Thanks!
> 
> > > On Wed, Jun 8, 2011 at 12:30 PM, Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > > 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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid).  
> To unsubscribe from this group, send email to  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid).  
> For more options, visit this group at  
> [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](http://www.fernandoaureliano.com) -  
[blog][http://www.fernandoaureliano.com/](http://www.fernandoaureliano.com/)

- [personal blog] [http://www.obsessivocompulsivo.com/](http://www.obsessivocompulsivo.com/) -  
[twitter][http://twitter.com/Pailoro](http://twitter.com/Pailoro)

---

<div class="post-metadata">

**Author:** ![fernandoaleman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/eb8c5e/32.png) [@fernandoaleman](https://www.ruby-forum.com/u/fernandoaleman)\
**Post date:** [June 8, 2011, 9:33pm UTC](https://www.ruby-forum.com/t/i-can-not-redirect-after-login-with-devise/208031/7 "2011-06-08T21:33:26Z")

</div>

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](http://example.com)”  
end

Thanks by the tips! 😉

On Wed, Jun 8, 2011 at 3:55 PM, Fernando A. \<  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> 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](http://railstutorial.org), which
> > > 
> > > > > > I’m started trying from the documentation
> > > 
> > > user
> > > 
> > > > > >
> > > 
> > > [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid).
> > > 
> > > > > 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](http://www.fernandoaureliano.com) -  
[blog][http://www.fernandoaureliano.com/](http://www.fernandoaureliano.com/)

- [personal blog] [http://www.obsessivocompulsivo.com/](http://www.obsessivocompulsivo.com/) -  
[twitter][http://twitter.com/Pailoro](http://twitter.com/Pailoro)

---

<div class="post-metadata">

**Author:** ![fernandoaleman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/eb8c5e/32.png) [@fernandoaleman](https://www.ruby-forum.com/u/fernandoaleman)\
**Post date:** [June 8, 2011, 5:58pm UTC](https://www.ruby-forum.com/t/i-can-not-redirect-after-login-with-devise/208031/8 "2011-06-08T17:58:57Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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](http://www.fernandoaureliano.com) -  
[blog][http://www.fernandoaureliano.com/](http://www.fernandoaureliano.com/)

- [personal blog] [http://www.obsessivocompulsivo.com/](http://www.obsessivocompulsivo.com/) -  
[twitter][http://twitter.com/Pailoro](http://twitter.com/Pailoro)
