Cayce Balara
2008-Mar-24 00:47 UTC
Custom actions with RESTful controllers - is this right?
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Arshak Navruzyan
2008-Mar-24 02:10 UTC
Re: Custom actions with RESTful controllers - is this right?
Cayce, What you''re seeing is correct. Per the documentation 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 Balara 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.-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Navruzyan <rails-mailing-l...@andreas- s.net> wrote:> Cayce, > > What you''re seeing is correct. Per the documentation > > http://api.rubyonrails.org/classes/ActionController/Resources.html#M0... > > 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 Balara 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. > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Cayce Balara
2008-Mar-24 13:02 UTC
Re: Custom actions with RESTful controllers - is this right?
Many thanks to both of you for your assistance. I''m seeing it much more clearly now. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---