Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3, Polymorphic Associations, and No Route Matches
    primarykey
    data
    text
    <p>I've been learning Rails for about 6 weeks now, so still a noob!</p> <p>I'm following Ryan Bates screencast on Polymorphic Associations, but I'm getting a "No Route Matches" error when navigating to /model/xx/comments.</p> <p>After two days going round in circles with this I'm totally stumped—everything appears to be in place.</p> <p>Comments model:</p> <pre><code>create_table "comments", :force =&gt; true do |t| t.text "content" t.integer "user_id" t.integer "commentable_id" t.string "commentable_type" t.datetime "created_at" t.datetime "updated_at" end </code></pre> <p>Comments class:</p> <pre><code>class Comment &lt; ActiveRecord::Base belongs_to :commentable, :polymorphic =&gt; true end </code></pre> <p>Other models class:</p> <pre><code>class ModelName &lt; ActiveRecord::Base has_many :comments, :as =&gt; :commentable end </code></pre> <p>Routes.rb</p> <pre><code>resources :modelname, :has_many =&gt; :comments </code></pre> <p>comments_controller.rb</p> <pre><code>def index @commentable = find_commentable @comments = @commentable.comments end private def find_commentable params.each do |name, value| if name =~ /(.+)_id$/ return $1.classify.constantize.find(value) end end nil end </code></pre> <p>This all according to the tutorial, but nonetheless is returning "no route matches".</p> <p>I have tried alternative formatting of the routes as nested resources.</p> <pre><code>resources :modelname do |modelname| modelname.resources :comments end </code></pre> <p>Explicitly defining comments in routes.rb</p> <pre><code>resources :comments </code></pre> <p>And various combinations of terms in routes.rb</p> <pre><code>resources :modelname, :has_many =&gt; :commentables </code></pre> <p>or</p> <pre><code>resources :modelname, :has_many =&gt; :comments </code></pre> <p>or</p> <pre><code>resources :modelname, :has_many =&gt; :comments, :through =&gt; :commentable </code></pre> <p>all without success.</p> <p>Has anyone else encountered this? I'm lost as to where to begin looking.</p> <p>Many thanks </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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