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

krn*_*njn 4 anchor ruby-on-rails erb link-to

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

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

<%= link_to "Leave a comment", [post.postable, post] %>
Run Code Online (Sandbox Code Playgroud)

由于我正在为帖子使用多态关联(并且它们是嵌套路由),因此我不能简单地使用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'}) %>
Run Code Online (Sandbox Code Playgroud)

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

Ste*_*fan 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') %>
Run Code Online (Sandbox Code Playgroud)