Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3 - Nested resources and polymorphic paths: OK to two levels, but break at three
    text
    copied!<p>I'm trying to do a simple family reunion site with: "posts", "families", "kids", and "pictures". Ideally I'd like the routes/relationships to be structured this way:</p> <pre><code> resources :posts do resources :pictures end resources :fams do resources :pictures resources :kids do resources :pictures end end </code></pre> <p>In the models I have the necessary "<code>belongs_to</code>" and "<code>has_many</code>" relationships set between <code>fams</code> and <code>kids</code>. <code>Fams</code>, <code>kids</code>, and <code>posts</code> all are defined with <code>"has_many :pictures, :as =&gt; :imageable</code>" while pictures are defined as: <code>belongs_to :imageable, :polymorphic =&gt; true</code></p> <p>When trying to do <code>link_to "Edit"</code> and <code>link_to "Destroy"</code> in the <code>pictures</code> views I run into all sorts of <code>_path</code> problems. <code>polymoric_path</code> works fine at two levels, namely for <code>posts-pictures</code> and <code>fams-pictures</code> but it fails to handle the three level case of <code>fams-kids-pictures</code>. I'm guessing that it was not designed to handle the two levels of "<code>imageable</code>" objects above the <code>picture</code> object. Another issue is that in one instance the <code>pictures</code> controller has to handle a "one level" resource-nesting situation and in another it has to handle a "two levels" situation. Not sure how to approach this.</p> <p>One thing I did try was to not nest resources more than one deep, per the Ruby Guides directions. I structured them like this:</p> <pre><code> resources :posts do resources :pictures end resources :fams do resources :pictures resources :kids end resources :kids do resources :pictures end </code></pre> <p>This caused another set of problems with paths since the fam to kid relationship was no longer preserved. I also could not get polymorphic_path to function correctly accross all the different <code>picture</code> views. </p> <p>So here is my main question: Does anyone know of a Rails 3 example/tutorial where nested resources, belongs-to/has_many, and polymorphic relationships are all put together, especially where it is not just the simple, two-level relationship that most examples show? (I'm fairly new to Rails and the Rails 2 examples I've found in these areas are confusing given my lack of Rails historical experience.)</p> <p>Or can someone tell me how to structure the <code>link_to EDIT</code> and <code>link_to DELETE</code> statements for my <code>picture</code> views, as well as the <code>redirect-to</code> statement for my <code>create</code>, <code>update</code>, and <code>destroy</code> methods in my <code>pictures</code> controller? </p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload