Erik Lindblad
2007-Oct-31 17:43 UTC
Keeping DRY while creating objects with nested REST models
I have rescently jumped on the REST-wagon and am enjoying it a lot. Now I have run into a problem: Lets say I have a model called Collection which has_many Items. I know (from Peep Code railscast, great stuff btw) how to create new Items belonging to a certain Collection. In other words I can create Items for an existing Collection. Now I want to do this: On the "create new collection" form I want to be able to create new Items without having to create the Collection first. This means the create action of collections_controller needs to create new Item objects. This can be done within the create method but this will violate the DRY principle. I would like to use the create action in the items_controller to create the new Items. Grateful on any input on how to do this in an elegant way. Kindest regards Erik Lindblad --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
randomutterings...
2007-Nov-01 12:39 UTC
Re: Keeping DRY while creating objects with nested REST models
What is to stop you from just using the items_controller to create the items? On Oct 31, 1:43 pm, Erik Lindblad <e...-gTO0lQh45js@public.gmane.org> wrote:> I have rescently jumped on the REST-wagon and am enjoying it a lot. > Now I have run into a problem: > > Lets say I have a model called Collection which has_many Items. I know > (from Peep Code railscast, great stuff btw) how to create new Items > belonging to a certain Collection. In other words I can create Items > for an existing Collection. Now I want to do this: > > On the "create new collection" form I want to be able to create new > Items without having to create the Collection first. This means the > create action of collections_controller needs to create new Item > objects. This can be done within the create method but this will > violate the DRY principle. I would like to use the create action in > the items_controller to create the new Items. > > Grateful on any input on how to do this in an elegant way. > > Kindest regards > > Erik Lindblad--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
monkeyten
2007-Nov-01 15:26 UTC
Re: Keeping DRY while creating objects with nested REST models
Maybe we have a similar issue, although I''m not quite sure: Lets say I have a RESTful resource called ''Messages''. Each Message has associated Categories. They are also CRUDable on their own, so I have another RESTful resource called ''Categories'' In my Message Controller I have a Create method, this obviously instantiates @message and @categories objects. The @categories object is essentially the same as is created by the Create method of the Categories controller. Is there any way to DRY this? Is it possible to call the Categories#create method from the Message controller without having it render the Categories form? If i have render :nothing in the Categories#create method then I cannot create a stand alone category (which is necessary). Apologies if my terminology is a bit awry... monkeyten On Oct 31, 5:43 pm, Erik Lindblad <e...-gTO0lQh45js@public.gmane.org> wrote:> I have rescently jumped on the REST-wagon and am enjoying it a lot. > Now I have run into a problem: > > Lets say I have a model called Collection which has_many Items. I know > (from Peep Code railscast, great stuff btw) how to create new Items > belonging to a certain Collection. In other words I can create Items > for an existing Collection. Now I want to do this: > > On the "create new collection" form I want to be able to create new > Items without having to create the Collection first. This means the > create action of collections_controller needs to create new Item > objects. This can be done within the create method but this will > violate the DRY principle. I would like to use the create action in > the items_controller to create the new Items. > > Grateful on any input on how to do this in an elegant way. > > Kindest regards > > Erik Lindblad--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Erik Lindblad
2007-Nov-13 01:03 UTC
Re: Keeping DRY while creating objects with nested REST models
Your situation seems very similar to mine. Actually I found a great answer to this in the #73-75 screencasts at http://railscasts.com/. Have a look, it helped me a lot. Thanks for the input. Regards Erik On 1 Nov, 16:26, monkeyten <philip.atkin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Maybe we have a similar issue, although I''m not quite sure: > > Lets say I have a RESTful resource called ''Messages''. > > Each Message has associated Categories. They are also CRUDable on > their own, so I have another RESTful resource called ''Categories'' > > In my Message Controller I have a Create method, this obviously > instantiates @message and @categories objects. The @categories object > is essentially the same as is created by the Create method of the > Categories controller. > > Is there any way to DRY this? Is it possible to call the > Categories#create method from the Message controller without having it > render the Categories form? If i have render :nothing in the > Categories#create method then I cannot create a stand alone category > (which is necessary). > > Apologies if my terminology is a bit awry... > > monkeyten >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---