2

I tried to follow this answer to a similar question but in my case I'm using Rolify.

The problem I'm running into is in passthrough_controller.rb where I can't access the Rolify .has_role? function:

class PassthroughController < ApplicationController
  def index
    if current_user.has_role? :admin
      redirect_to 'restaurants#index'
    else
      redirect_to 'http://www.google.com'
    end
  end
end

I have logged in using a user that I know has an admin role but it still redirects me to my else clause.

I couldn't find anything on the Rolify github page on how to do this.

Any help would be much appreciated

Community
  • 1
  • 1
FilmiHero
  • 2,306
  • 7
  • 31
  • 46
  • Does current_user definitely work and not return null? Are you using Devise and CanCan? – AJcodez Jul 10 '12 at 21:09
  • I'm pretty sure `current_user` works because if I tried `current_user.role` I would get `NoMethodError in PassthroughController#index` with `undefined method 'role' for #< User:0x007fd0ddd3b070>` message. And yes, I'm using Devise and CanCan. – FilmiHero Jul 10 '12 at 21:13
  • Did you generate the role model and follow all the steps on the readme? – AJcodez Jul 10 '12 at 21:15
  • Also try adding the role to the current user in the index to make sure that is working. current_user.add_role :admin – AJcodez Jul 10 '12 at 21:17
  • Yup. My Rolify stuff works perfectly throughout my website. – FilmiHero Jul 10 '12 at 21:19
  • I did `current_user.add_role :admin` then the usual `if current_user.has_role? :admin ... ` but I got a `Error 324 (net::ERR_EMPTY_RESPONSE): The server closed the connection without sending any data.` from my Chrome browser. – FilmiHero Jul 10 '12 at 21:23
  • Hmm. See if your admin shows up with this: admins = Role.find_by_name('admin').users just print the list in a view. – AJcodez Jul 10 '12 at 21:25
  • I ran it in my Rails Console and got back the correct user info. – FilmiHero Jul 10 '12 at 21:29
  • Im not sure :( the method could not be available because it needs to be included in ApplicationController is my only thought but I dont know how to include it. Sorry! – AJcodez Jul 10 '12 at 21:37

2 Answers2

0

The solution was actually very simple.

I just needed to prefix my restaurants#index with a / so my code should've looked like this:

  def index
    if current_user.has_role? :admin
      redirect_to '/restaurants#index'
    else
      redirect_to 'http://www.google.com'
    end
  end

I didn't have the prefix in the beginning because that's how I defined my root in my routes.rb (root :to => 'restaurants#index')

FilmiHero
  • 2,306
  • 7
  • 31
  • 46
  • You could also use [resource routes](http://guides.rubyonrails.org/routing.html) to improve your example. – JJD Nov 28 '12 at 12:27
  • 3
    This answer can be very misleading to beginners. What you do here is redirecting to '/restaurants' path with 'index' anchor. The syntax in routes.rb file is different from how one should access routes in the controllers. Please change the first redirect to restaurants_path and correct the explanation. – mdrozdziel Sep 22 '13 at 11:09
0

it seems like you could just use render to do this as opposed to uses either routes or redirects.

def index
    if current_user.has_role? :admin
      render "admin" # where admin.html.erb is in the same folder as index.html.erb
    else
      # do nothing - render default view
    end
end