加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 使用数组在Rails link_to中添加锚选项

发布时间:2020-12-17 02:37:21 所属栏目:百科 来源:网络整理
导读:我正在使用数组为我的rails link_to标签生成路径,似乎无法弄清楚如何添加锚选项.这是我的link_to标签: %= link_to pluralize(post.comments.count,'comment'),[post.postable,post] %%= link_to "Leave a comment",post] % 由于我正在为帖子使用多态关联(并
我正在使用数组为我的rails link_to标签生成路径,似乎无法弄清楚如何添加锚选项.这是我的link_to标签:

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

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

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

以前,我能够在自动生成的路径上使用锚选项,因为我没有使用与此模型的多态关联.这是看起来像:

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

<%= link_to "Leave a comment",{anchor: 'new-comment'}) %>

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

解决方法

你可以拨打 polymorphic_path

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

<%= link_to "Leave a comment",anchor: 'new-comment') %>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读