Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if can? in Ruby on Rails

In a Ruby on Rails application that I inherited from someone, I have code that looks like so

<% if can? :create, :objects %>
<%= link_to 'Add New Object', new_object_path %>

This web application has a login and users have different permissions that are defined in a table called groups_roles (which groups (ex. admin, user) have which roles (ex. add new objects))

I want to add new permissions, so where do I do that at? Where are these things defined? How does Ruby know which table to get the different permissions from, and how does it know what :create and :objects are in the code above?

like image 544
CodeGuy Avatar asked Jun 02 '13 17:06

CodeGuy


2 Answers

The app seems to be using the cancan gem by ryan bates. You can specify the permissions in the app/models/ability.rb file.

It simply reads the ability file to determine if a user can perform some action or not. These actions correspond directly to the actions you have defined in the controller class.

Cancan has a great wiki at its github repositiory. Also, the screencast by ryan is an excellent place to start off with.

like image 54
Nerve Avatar answered Nov 14 '22 21:11

Nerve


I do not know how the application is working, but the can? comes from the cancan gem. See the screencast.

like image 24
Ven Avatar answered Nov 14 '22 22:11

Ven