4

我正在使用一个数组为我的 rails link_to 标签生成路径,但似乎无法弄清楚如何添加锚选项。这是我的 link_to 标签:

<%= link_to pluralize(post.comments.count, 'comment'), [post.postable, post] %>

<%= link_to "Leave a comment", [post.postable, post] %>

由于我对帖子使用多态关联(并且它们是嵌套路由),因此我不能简单地使用由 routes.rb 文件中的资源助手生成的路径。

以前,我能够在自动生成的路径上使用锚选项,因为我没有使用与此模型的多态关联。这就是它的样子:

<%= link_to pluralize(post.comments.count, 'comment'), project_post_path(@project, post, {anchor: 'comments'}) %>

<%= link_to "Leave a comment", project_post_path(@project, post, {anchor: 'new-comment'}) %>

关于在使用数组生成 url 时如何将锚标记返回到 link_to 标记的任何提示?提前致谢。

4

2 回答 2

5

您可以致电polymorphic_path

<%= link_to pluralize(post.comments.count, 'comment'), polymorphic_path([post.postable, post], anchor: 'comments') %>

<%= link_to "Leave a comment", polymorphic_path([post.postable, post], anchor: 'new-comment') %>
于 2013-02-20T15:18:45.417 回答
0

试试这个:

<%= link_to "Leave a comment", [post.postable, post], :anchor=> 'new-comment' %>
于 2012-11-01T18:40:46.147 回答