繁体   English   中英

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

[英]Add anchor option in Rails link_to using array

我正在使用数组为我的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标记的任何提示? 提前致谢。

你可以调用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') %>

试试这个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM