Search code examples
ruby-on-railscancannested-resources

Nested Resources vs/and/or CanCan?


I am using Rails 3 with the CanCan gem. The models Users and Networks are a has_many :through => Roles. This part works fine.

Currently I am using CanCan so that on the /networks page it will only display a network if a users has access to it. This also works fine.

I know that CanCan can be combined with nested resources, but is having a route for /users/1/networks redundant? optional? Encouraged? RESTful? Rails-y? Is there any specific reason to have it? I only want users to be able to see networks they have access to, but wasn't sure if nested resources was the more proper way to go if I already have the Network index acting as a filter.


Solution

  • To me, the route should make sense in the context of the people using your application. If you're logged in as an admin that can CRUD routes I would want this done through /networks because all things you're doing related to routes will occur here.

    /networks            #list all networks
    /networks/new        #add network
    /networks/1          #show network
    /networks/1/edit     #edit network
    

    From a user perspective, however, it makes sense for them to be able to add/remove them self to a new network or view the network they're in.

    /users/1/networks         #show networks user is in
    /users/1/networks/add     #add self to network
    /users/1/networks/delete  #remove self from network