From: Mike Stok Date: 2012-04-10T20:47:47+09:00 Subject: Re: Best way of implementing controllers for m:n model relation (Car->Parts) On 2012-04-08, at 1:19 PM, Thomas Tschofenig wrote: > Hello, > > in the last few weeks I`ve taken a clooser look on Ruby and I am > actually impressed on how helpful this "toolset" is. > > However, prior to ruby I was mainly working with functional languages > like ASP and PHP for web development. > > So my question to you is an archtictural one, as i`m having some > difficulties on how to implement the MVC model correctly: > > Let`s say I have a model "Car" and "Part". A car can have one or more > parts (m:n). > > On a specific page the user should be able to change the cars details > and it`s parts (add part, remove part, set quantity etc.). > > How would you implement the controllers ? > > I would have used the following approach but ran into several problems: > > CarsController > --> index > --> show > --> new (needs PartsController/index to display parts) > --> create > --> edit > --> update > > PartsController > --> index (displays all parts of a car) > --> show (displays single part of a car) > --> new (add part) > --> create > --> edit (set quantity) > --> update > > When I tried to embed the template of "PartsController/index" into the > template "CarsController/new" by using the "render" helper in the > template it doesn`t work. > > Example (CarsController/new.html.erb): <%= render @car.pars %> // empty > output > > Is this approach the most common way to implement this or I am wrong ? > IF it is correct how do I include another action of another controller > in a view ? > > Please be kind, I am a newbie trying the (correct) MVC way... This looks like a Rails question, and one place to start looking for Rails answers is at http://railscasts.com. Does http://railscasts.com/episodes/196-nested-model-form-part-1 give you any ideas? Regards, Mike -- Mike Stok http://www.stok.ca/~mike/ The "`Stok' disclaimers" apply.