where is user_can? method and how does it work?

48 views
Skip to first unread message

梦巧 张

unread,
Aug 29, 2011, 11:08:43 PM8/29/11
to cantango
Hi, I'm a quite freshman on rails, and of course also on cantango,
cancan, and devise.
I am trying to use cantango with devise to implement a user management
and the access control. I follow the quick start on Github, but i got
the problem as follows:

app/views/posts/index.html.erb where line #15 raised:

undefined method `user_can?' for #<#<Class:0x20037bc>:0x20029e8>


<%= Index.all.inspect if user_can?(:read, Post) %>
<td><%= link_to 'Edit', edit_post_path(post) if user_can?(:edit, Post)
%></td>

I'm confused where do you define the 'user_can?' method? and how does
this method work?

Hope you can give me a feedback. You know there is quite few issues
about cantango.... Thank you!!!

Kristian Mandrup

unread,
Aug 30, 2011, 12:38:43 PM8/30/11
to cantango
When you have the gem can tango in your Gemfile and you run

$ bundle instal

then Rails should include can tango like any other gem. Part of
cantango, if you look into the source is a so called Rails Engine (see
lib/cantango/rails/engine.rb). The engine should include the main
cantango API methods into views and controllers for each registered
user and account. For this to work, you have to register your User and/
or Account classes.
See the dummy apps for examples of how to do this:

class User
tango_user
end

class Admin < User
tango_user
end

these are so called class level macro methods. You can check if the
classes have been registered via can tango config.

CanTango.config.users.registered
CanTango.config.account.registered

I hope this helps. I'm currently on vacation in Marocco, surfing, but
you can also ask the other main contributor, @stanislaw. Good luck!

Kristian

梦巧 张

unread,
Aug 30, 2011, 9:42:23 PM8/30/11
to cantango
Thanks for your kind help!!
But I can't find lib/cantango/rails/engine.rb in my app, only lib/
tasks, even though I entered gem 'cantango' in my Gemfile and run
$bundle install. Is the cantango engine installed automatically by
bundle? or I should add it manually?
(Sorry for interrupting your nice surfing trip, anyway, thank you for
your help very much and enjoy your vacation.)

张梦巧

unread,
Aug 31, 2011, 2:12:59 AM8/31/11
to cantango
Sorry for asking the stupid question. I got that where the engine.rb is. 

But for my original problem about user_can? I still not yet fixed after adding 'tango_user' to the app/models/user.rb and  CanTango.config.users.registered to cantango.rb... 

I watched through nearly all the documents on github, but still have no idea how to use cantango... I think it's easy understanding for the professional rails programmer, but for the freshman, more detail steps (I mean step by step but not general explanations or guideline) might be more helpful.

Anyway, thanks very much for your help again. Expecting more detail steps and the completed demo. Nice vacation. 

2011/8/31 梦巧 张 <zmq...@gmail.com>



--
Mengqiao Zhang (張夢巧)
Graduate School,
University of Aizu, Japan

Reply all
Reply to author
Forward
0 new messages