0

I have an app that uses Rolify, Devise, CanCan, and Tabulous.

In my tabulous.rb file, I want to be able to define the Visibility? of a tab by calling:

(current_user.has_role? :admin)

So the config.tabs do will look something like this:

# TAB NAME | DISPLAY TEXT | PATH | VISIBILE? | ENABLED?
[ :admin_tab, 'ADMIN' , ingredients_path , (current_user.has_role? :admin) , true ],

Which works fine for most of the pages EXCEPT for when I go into the /users/sign_in path; it returns the following error:

NoMethodError in Devise/sessions#new

Showing /Users/[myname]/Projects/[project-name]/app/views/layouts/application.html.haml where line #24 raised:

undefined method `has_role?' for nil:NilClass

And line #24 just points to the =tabs call within the applications.html file.

I know the problem is that /users/sign_in doesn't have access to the .has_role? function.

How can I make it so that the tabulous.rb file can use it?

FilmiHero
  • 2,306
  • 7
  • 31
  • 46

3 Answers3

2

I've tried this code and worked!

config.tabs do
  showTab = (current_user && (current_user.has_role? :admin))
  [
    [ :users_tab, "Users", users_path, showTab, showTab],
  ]
end
ffscalco
  • 541
  • 4
  • 14
0

If I'm not mistaken, tabulous caches tabulous.rb when in production mode, so while your approach might work most of the time in development, it would break in production, and everyone would end up getting the same tabs, one way or the other.

Loren
  • 3,476
  • 3
  • 23
  • 15
  • Thanks for the response @Karen. I worked around the issue a while back by reassessing the users' accessibility. – FilmiHero Aug 01 '12 at 16:45
0

A simple fix can be done in `tabulous.rb file

[:admin_tab, 'Admin', users_path, current_user && current_user.has_role?(:admin), true],

current_user is not defined when the user is signed out, so you need to also check in tabulous.rb file if there is actually a current_user defined.

user14412
  • 987
  • 1
  • 11
  • 32