2014-01-20 44 views
0

我正在使用rails 4.rails link_to將post.id添加到href

有什麼方法可以將post.id添加到link_to href?

<%= link_to_modal "Demo Form", "#demo-form-"+post.id, :class=>"button" %> 
and 
<%= link_to_modal "Demo Form", "#demo-form-"(post.id), :class=>"button" %> 

不起作用。

+1

我不確定你在問什麼。你試過了:'<%= link_to_modal「Demo Form」,「#demo-form - #{post.id}」,:class =>「button」%>'? – Surya

+0

你說得對。而已。工作完美!謝謝 – AlexEfremo

+1

'post.id.to_s'也應該有效:'<%= link_to_modal「Demo Form」,「#demo-form - 」+ post.id.to_s,:class =>「button」%>',但是我更喜歡@ Surya的答案! – vee

回答

2

試試這個:

<%= link_to_modal "Demo Form", "#demo-form-#{post.id}", :class=>"button" %>

1

添加此答案指出錯誤,以及如何你可以有你的現有代碼修復它。

這一行的唯一問題:

<%= link_to_modal "Demo Form", "#demo-form-"+post.id, :class=>"button" %> 

的是,你要一個Concat的與Fixnum其中string應該已經產生can't convert Fixnum into String錯誤。

您也可以使用post.id.to_s解決你已經有了爲:

<%= link_to_modal "Demo Form", "#demo-form-" + post.id.to_s, :class=>"button" %> 

而且你的第二行:

<%= link_to_modal "Demo Form", "#demo-form-"(post.id), :class=>"button" %> 

是無效的,應該拋出SyntaxError

我不確定你的選擇的語法,但我喜歡@ Surya的答案更好,使用字符串插值。