在Rails的link_to中使用数组添加锚点选项

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标签中的提示吗?提前致谢。
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') %>

0

试试这个:

<%= link_to "Leave a comment", [post.postable, post], :anchor=> 'new-comment' %>

那行不通。我之前尝试过,它根本没有添加任何东西。 - krnjn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接