Polymorphic_url链接嵌套注释资源路由?

时间:2014-10-14 18:40:13

标签: ruby-on-rails ruby ruby-on-rails-4 routing

我无法让我的嵌套路由按照我希望的方式工作。

resources :posts do
  member do
    put "like", to: "posts#upvote"
    put "dislike", to: "posts#downvote"
  end
  resources :comments do
  member do
    put "like", to: "comments#upvote"
    put "dislike", to: "comments#downvote"
  end
end
end

如何使用polymorphic_url / path链接到这些?我已阅读API但我仍然遇到问题?

目前在我的帖子索引视图中:

   <div id="posts">
   <% @posts.each do |post| %>
    <h2><%= link_to post.title, post %></h2>
    <%= simple_format post.content %>
    <%= current_user.username %>
    <p>
      Tag: <%= raw post.tag_list.map { |t| link_to t, tag_path(t) }.join(', ') %>
     </p>

      <div>
  <%= link_to "up", like_post_path(post), method: :put %>
  <%= link_to "down", dislike_post_path(post), method: :put %>
  <%= post.score %>
  </div>

  <p><%= link_to "Edit Post", edit_post_path(post) %></p>
  <p><%= link_to "Comment", new_polymorphic_url([post, @comment]) %></p>
  <p><%= link_to "View Comments", polymorphic_url([post, @comments]) %></p>
  <% end %>
  </div>

现在“评论”链接将您带到new_post_path

和“查看评论”链接将您带到posts_path

1 个答案:

答案 0 :(得分:0)

您可以更改这些行:

<p><%= link_to "Comment", new_polymorphic_url([post, @comment]) %></p>
<p><%= link_to "View Comments", polymorphic_url([post, @comments]) %></p>

到此:

<p><%= link_to "Comment", new_post_comment_path(post) %></p>
<p><%= link_to "View Comments", post_comments_path(post) %></p>