2012-11-01 29 views
4

我正在使用數組來生成我的rails link_to標記的路徑,並且似乎無法弄清楚如何添加錨定選項。這裏是我的link_to標籤:在Rails 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'}) %> 

如何獲得錨標記回的link_to標記任何提示使用陣列生成的URL是什麼時候?提前致謝。

回答

4

您可以撥打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') %> 
+0

謝謝@stefan。這解決了它! – krnjn

0

試試這個:

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

不起作用。我之前嘗試過,它根本不添加任何東西。 – krnjn