Search code examples
ruby-on-railsrailscasts

Rails polymorphic association link not working


I'm trying to set up a polymorphic association, and I seem to have everything working except this one thing. I'm following along with the railscast here https://www.youtube.com/watch?v=WOFAcbxdWjY and there is a section where he adds a link to a new comment that's linked to the photo.

The code he listed worked in the video just fine. In the video the [:new, @commentable, @comment] line makes the link go to ..photos/1/comments/new

Here is what I have in my comments view.

<div id="wrapper">
    <h3>Comments</h3>
    <p><%= link_to "New Comment", [:new, @commentable, @comment] %></p>
    <% @comments.each do |comment| %>
      <div class="comments">
        <div class="post-title"><%= comment.content %></div>
      </div>
    <% end %>
</div>

Only thing is that when I do this, the link points to

..articles/new.4

instead of ..articles/4/comments/new

What am I doing wrong? I'm also using rails5.


Solution

  • It seems that your problem is a typo, you are using @comment instead of :comment.

    Try changing:

    <p><%= link_to "New Comment", [:new, @commentable, @comment] %></p>
    

    to:

    <p><%= link_to "New Comment", [:new, @commentable, :comment] %></p>