简体   繁体   English

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

[英]Add anchor option in Rails link_to using array

I'm using an array to generate the path for my rails link_to tags and can't seem to figure out how to add an anchor option. 我正在使用数组为我的rails link_to标签生成路径,似乎无法弄清楚如何添加锚选项。 Here are my link_to tags: 这是我的link_to标签:

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

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

Since I'm using a polymorphic association for the posts (and they are nested routes), I can't simply use the paths generated by the resources helpers in the routes.rb file. 由于我正在为帖子使用多态关联(并且它们是嵌套路由),因此我不能简单地使用routes.rb文件中的资源助手生成的路径。

Previously, I was able to use the anchor option on the paths automagically generated as I was not using the polymorphic association with this model. 以前,我能够在自动生成的路径上使用锚选项,因为我没有使用与此模型的多态关联。 This is what that looked like: 这是看起来像:

<%= 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'}) %>

Any tips on how to get an anchor tag back into the link_to tags when using an array to generate the url? 有关如何在使用数组生成url时将锚标记返回到link_to标记的任何提示? Thanks in advance. 提前致谢。

You can call polymorphic_path : 你可以调用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