簡體   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