Hi, Today I added a checkbox to each row of a table and a save button, to perform an Archive operation on N items in the table. This is essentially an update of the collection. My initial thought was to PUT to students_path but only GET and POST are allowed using the Rails RESTful routing. I wound up adding an :collection => {:update_all => :put} to the students routing and added an appropriate controller method. I''m not terribly happy with this, stylistically, though, pragmatically, it works. What is the proper way to manage an update to a collection w/o having to add an addition method to my controller? Thanks very much, -Tim -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
CrazyScorpio12
2010-Aug-09 04:56 UTC
[Rails] Re: Updating a collection in a RESTful manner…
I would also like to know the correct way of doing a collection update. but I, generally, use ajax for check box, so when user clicks on the yes - no check box I make a put request to update 1 record at a time. Aashish On Aug 8, 2:07 am, Tim Harding <tim.hard...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > Today I added a checkbox to each row of a table and a save button, to > perform an Archive operation on N items in the table. > > This is essentially an update of the collection. > > My initial thought was to PUT to students_path but only GET and POST > are allowed using the Rails RESTful routing. > > I wound up adding an :collection => {:update_all => :put} to the > students routing and added an appropriate controller method. > > I''m not terribly happy with this, stylistically, though, > pragmatically, it works. > > What is the proper way to manage an update to a collection w/o having > to add an addition method to my controller? > > Thanks very much, > > -Tim-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 7 August 2010 22:07, Tim Harding <tim.harding-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Today I added a checkbox to each row of a table and a save button, to > perform an Archive operation on N items in the table. > > This is essentially an update of the collection. > > My initial thought was to PUT to students_path but only GET and POST > are allowed using the Rails RESTful routing. > > I wound up adding an :collection => {:update_all => :put} to the > students routing and added an appropriate controller method. > > I''m not terribly happy with this, stylistically, though, > pragmatically, it works. > > What is the proper way to manage an update to a collection w/o having > to add an addition method to my controller?I don''t think you can avoid having an additional method in the controller -- after all, the code required to update the collection is going to be different from any of your other actions. But you can avoid exposing the name of that new method in the URL. I would try manually defining a new PUT route outside of the existing resource map: map.connect ''students'', :controller => ''students'', :action => ''update_collection'', :conditions => {:method => :put} That should route correctly if you have a form with :url => students_path, :method => :put. There may be a cleverer/neater way to do this within the resource map itself, but this at least is a good first thing to try. Chris -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Here is an advise for "What is the Restful manner for updating a collection?" http://rails-bestpractices.com/questions/3-what-is-the-restful-manner-for-updating-a-collection On 8月9日, 下午5时52分, Chris Mear <chrism...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 7 August 2010 22:07, Tim Harding <tim.hard...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Today I added a checkbox to each row of a table and a save button, to > > perform an Archive operation on N items in the table. > > > This is essentially an update of the collection. > > > My initial thought was to PUT to students_path but only GET and POST > > are allowed using the Rails RESTful routing. > > > I wound up adding an :collection => {:update_all => :put} to the > > students routing and added an appropriate controller method. > > > I''m not terribly happy with this, stylistically, though, > > pragmatically, it works. > > > What is the proper way to manage an update to a collection w/o having > > to add an addition method to my controller? > > I don''t think you can avoid having an additional method in the > controller -- after all, the code required to update the collection is > going to be different from any of your other actions. But you can > avoid exposing the name of that new method in the URL. > > I would try manually defining a new PUT route outside of the existing > resource map: > > map.connect ''students'', :controller => ''students'', :action => > ''update_collection'', :conditions => {:method => :put} > > That should route correctly if you have a form with :url => > students_path, :method => :put. > > There may be a cleverer/neater way to do this within the resource map > itself, but this at least is a good first thing to try. > > Chris-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Tim Harding
2010-Aug-11 05:13 UTC
Re: [Rails] Re: Updating a collection in a RESTful manner…
@flyerhzm: I quite like the idea of updating the owner of the collection in the case that there is one. There certainly was in my case, but it feels weird to PUT from /school/students to /school, for example. @chrismear: Your additional route seems cleaner than the :collection => {:update_all => :put} but the Wikipedia entry for REST seems to suggest that a PUT to the collection URL should _replace_ the entire collection with another collection so perhaps merely updating some attributes of some of the collection breaks architectural agreement. On 10 August 2010 09:27, flyerhzm <flyerhzm-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Here is an advise for "What is the Restful manner for updating a > collection?" > > > http://rails-bestpractices.com/questions/3-what-is-the-restful-manner-for-updating-a-collection > > On 8月9日, 下午5时52分, Chris Mear <chrism...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > On 7 August 2010 22:07, Tim Harding <tim.hard...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > > Today I added a checkbox to each row of a table and a save button, to > > > perform an Archive operation on N items in the table. > > > > > This is essentially an update of the collection. > > > > > My initial thought was to PUT to students_path but only GET and POST > > > are allowed using the Rails RESTful routing. > > > > > I wound up adding an :collection => {:update_all => :put} to the > > > students routing and added an appropriate controller method. > > > > > I''m not terribly happy with this, stylistically, though, > > > pragmatically, it works. > > > > > What is the proper way to manage an update to a collection w/o having > > > to add an addition method to my controller? > > > > I don''t think you can avoid having an additional method in the > > controller -- after all, the code required to update the collection is > > going to be different from any of your other actions. But you can > > avoid exposing the name of that new method in the URL. > > > > I would try manually defining a new PUT route outside of the existing > > resource map: > > > > map.connect ''students'', :controller => ''students'', :action => > > ''update_collection'', :conditions => {:method => :put} > > > > That should route correctly if you have a form with :url => > > students_path, :method => :put. > > > > There may be a cleverer/neater way to do this within the resource map > > itself, but this at least is a good first thing to try. > > > > Chris > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- Tim Harding Well Informed Ltd Registered in England & Wales Company number 06707839 Registered office: Suite 235, 77 Beak St, London, W1F 9DB -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.