# Custom actions with RESTful controllers - is this right?

**URL:** <https://www.ruby-forum.com/t/custom-actions-with-restful-controllers-is-this-right/132721>\
**Category:** Rails\
**Created:** [March 24, 2008, 1:47am UTC](https://www.ruby-forum.com/t/custom-actions-with-restful-controllers-is-this-right/132721 "2008-03-24T01:47:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![yardboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/9de0a6/32.png) [@yardboy](https://www.ruby-forum.com/u/yardboy)\
**Post date:** [March 24, 2008, 1:47am UTC](https://www.ruby-forum.com/t/custom-actions-with-restful-controllers-is-this-right/132721/1 "2008-03-24T01:47:41Z")

</div>

I was having some problems with custom actions in my controllers for  
RESTful resources. For instance, in my controller I have the following:

class VotersController \< ApplicationController  
def mark\_duplicates  
blah  
end  
end

…and in my routes.rb I have:

map.resources :voters

This doesn’t work, when I navigate to /voters/mark\_duplicates I get a  
“cannot find voter with id=mark\_duplicates”. Same with other actions  
“mark\_household” and “set\_filters” - obviously a conflict with the  
RESTful routes.

So, after some research I have changed my route definition to this:

map.resources :voters, :collection =\> { :mark\_duplicate =\> :get,  
:mark\_household =\> :get, :set\_filters =\> :get }

And now those actions work just fine. I just don’t understand why and  
I’m not sure if this is right or if there is anything else that I’m  
triggering with this code.

Appreciate any insight. Thanks in advance.

---

<div class="post-metadata">

**Author:** ![yardboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/9de0a6/32.png) [@yardboy](https://www.ruby-forum.com/u/yardboy)\
**Post date:** [March 24, 2008, 11:40am UTC](https://www.ruby-forum.com/t/custom-actions-with-restful-controllers-is-this-right/132721/2 "2008-03-24T11:40:59Z")

</div>

Methods added to an resource with :collection generate routes that  
don’t need any id to be executed. You can use those for methods that  
affect not just one specific voter, but for the voter-population as a  
whole. Methods added with :member need an id to be routed correctly,  
as they operate on a particular voter.

Two good practices: use the helpers that are generated with the  
resources (new\_voter\_path for example: \<%= link\_to ‘new’,  
new\_voter\_path %\>) and use “rake routes” in your console (in the root  
of your rails app) to see a list of all the generated routes. you can  
also see clearly then what those manually added methods do to your  
routes.

On Mar 24, 3:10 am, Arshak N. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![yardboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/9de0a6/32.png) [@yardboy](https://www.ruby-forum.com/u/yardboy)\
**Post date:** [March 24, 2008, 3:10am UTC](https://www.ruby-forum.com/t/custom-actions-with-restful-controllers-is-this-right/132721/3 "2008-03-24T03:10:00Z")

</div>

Cayce,

What you’re seeing is correct. Per the documentation

[http://api.rubyonrails.org/classes/ActionController/Resources.html#M000309](http://api.rubyonrails.org/classes/ActionController/Resources.html#M000309)

map.resources :voter

maps the “default” actions for that controller which are new, create,  
show, edit, update, destroy, index.

Any other action that you’d like to provide a route to has to be listed  
in  
:collection, :new, :member, etc.

Arshak

Cayce B. wrote:

> I was having some problems with custom actions in my controllers for  
> RESTful resources. For instance, in my controller I have the following:
> 
> class VotersController \< ApplicationController  
> def mark\_duplicates  
> blah  
> end  
> end
> 
> …and in my routes.rb I have:
> 
> map.resources :voters
> 
> This doesn’t work, when I navigate to /voters/mark\_duplicates I get a  
> “cannot find voter with id=mark\_duplicates”. Same with other actions  
> “mark\_household” and “set\_filters” - obviously a conflict with the  
> RESTful routes.
> 
> So, after some research I have changed my route definition to this:
> 
> map.resources :voters, :collection =\> { :mark\_duplicate =\> :get,  
> :mark\_household =\> :get, :set\_filters =\> :get }
> 
> And now those actions work just fine. I just don’t understand why and  
> I’m not sure if this is right or if there is anything else that I’m  
> triggering with this code.
> 
> Appreciate any insight. Thanks in advance.

---

<div class="post-metadata">

**Author:** ![yardboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/9de0a6/32.png) [@yardboy](https://www.ruby-forum.com/u/yardboy)\
**Post date:** [March 24, 2008, 2:02pm UTC](https://www.ruby-forum.com/t/custom-actions-with-restful-controllers-is-this-right/132721/4 "2008-03-24T14:02:46Z")

</div>

Many thanks to both of you for your assistance. I’m seeing it much more  
clearly now.
