ddonche
ddonche

Reputation: 1065

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.

Upvotes: 0

Views: 134

Answers (2)

Ken Stipek
Ken Stipek

Reputation: 1522

I believe you want to use the polymorphic_path helper, try this out:

<p><%= link_to "New Comment", new_polymorphic_path([@commentable, @comment]) %></p>

Upvotes: 1

Gerry
Gerry

Reputation: 10497

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>

Upvotes: 2

Related Questions