2017-05-04 3 views
0

Ich versuche, eine polymorphe Assoziation einzurichten, und ich habe alles funktioniert, außer diese eine Sache. Ich folge mit dem Railscast hier https://www.youtube.com/watch?v=WOFAcbxdWjY und es gibt einen Abschnitt, wo er einen Link zu einem neuen Kommentar hinzufügt, der mit dem Foto verknüpft ist.Rails polymorphe Association Link funktioniert nicht

Der Code, den er aufgelistet hat, funktionierte im Video gut. In dem Video die [:new, @commentable, @comment] Linie macht den Link gehen zu ..photos/1/comments/new

Hier ist, was ich in meiner comments Ansicht habe.

<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> 

Nur Sache ist, dass, wenn ich das tue, der Link verweist auf

..articles/new.4

statt ..articles/4/comments/new

Was mache ich falsch? Ich benutze auch rails5.

Antwort

2

Es scheint, dass Ihr Problem ein Tippfehler ist, verwenden Sie @comment anstelle von :comment.

Versuchen Wechsel:

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

zu:

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

, dass das Problem war, danke. – ddonche

1

Ich glaube, Sie polymorphic_path Helfer verwenden möchten, versuchen Sie dies aus:

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