# REST Design Question

**URL:** <https://www.ruby-forum.com/t/rest-design-question/125812>\
**Category:** Rails\
**Created:** [January 12, 2008, 9:42am UTC](https://www.ruby-forum.com/t/rest-design-question/125812 "2008-01-12T09:42:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pankaj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a9adbd/32.png) [@pankaj](https://www.ruby-forum.com/u/pankaj)\
**Post date:** [January 12, 2008, 9:42am UTC](https://www.ruby-forum.com/t/rest-design-question/125812/1 "2008-01-12T09:42:42Z")

</div>

I have 2 models Roles and Rights with many to many Relationship.

Without Rest:  
Controller: Action  
Roles: list,edit,create,destroy,map\_rights  
Rights: list,edit,create,destroy,list\_by\_role

NOTE: map rights function will map/unmap multiple rights to a  
particular role using a list of checkboxes.

What would be the design following Rest ?

Regards,  
Pankaj

---

<div class="post-metadata">

**Author:** ![pankaj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a9adbd/32.png) [@pankaj](https://www.ruby-forum.com/u/pankaj)\
**Post date:** [January 12, 2008, 5:29pm UTC](https://www.ruby-forum.com/t/rest-design-question/125812/2 "2008-01-12T17:29:44Z")

</div>

With REST:

Roles: index, new, create, edit, update, destroy  
Rights: index, new, create, edit, update, destroy

Now, I guess, an explanation.

List and index should be interchangeable; just get rid of ‘list’ for  
‘index’.

I assume there’s a means of creating these rights/roles from the first  
place. This form would usually be in a ‘new’ action (the form POSTs to  
‘create’). And while ‘edit’ is good to provide a GET form for a  
record, the form would post (using PUT) to ‘update’ to write the  
changes. ‘Destroy’ stays.

‘map\_rights’ seems to be an ‘update’ action if it’s modifying that  
role’s rights.

And finally, ‘list\_by\_role’ could be achieved with the following  
established routes:

# config/routes.rb

map.resources :roles, :has\_many =\> :rights  
map.resources :rights

Now you have role\_rights\_path(@role), which will allow you to use the  
RightsController#index action to list just the rights of a particular  
role, as long as @role is established from params[:role\_id]. You could  
just detect with a before\_filter:

# app/controllers/rights\_controller.rb

before\_filter :get\_rights

protected

def get\_rights  
if @role = Role.find params[:role\_id]  
@rights = @role.rights  
else  
@rights = Right.find :all  
end  
end

Now, the following two URLs will ‘list’ rights and ‘list\_by\_role’ with  
ID 1  
[http://0.0.0.0:3000/rights](http://0.0.0.0:3000/rights)  
[http://0.0.0.0:3000/roles/1/rights](http://0.0.0.0:3000/roles/1/rights)

Now, go convert all those crudful controllers!
