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
  end
end

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!

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?

Colin

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

=(

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!

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!

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! :wink: