【问题标题】:Polymorphic_url links for nested comment resource routes?嵌套评论资源路由的多态 URL 链接?
【发布时间】:2014-10-14 18:40:12
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

    您可以更改这些行:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多