2016-02-24 25 views
0

我正在關注this SE問題,將表單放入引導程序模式並使用rails。 回答問題的人聲明:「確保每個模態身體都有唯一的ID或類。」所以我想在我的link_to中添加一個唯一的ID號碼:link_to與嵌入式ruby中的數據切換

<%= link_to "Edit", edit_post_path(post.id), :class => "btn", :remote => true, "data-toggle" => "modal", "data-target" => "<%= post.id %>-my-modal" %> 

但是這樣做會導致錯誤。如果我拿出<%= post.id%>我沒有錯誤,但模式行爲不起作用。

如何添加帶有嵌入式ruby的post.id到鏈接?

回答

2

你必須把它寫這樣的:

<%= link_to "Edit", edit_post_path(post.id), :class => "btn", :remote => true, "data-toggle" => "modal", "data-target" => "#{post.id}-my-modal" %> 
1

一旦你打開一個<%...%>標籤,你正在編寫Ruby代碼。這意味着你不能嵌套<%...%>標籤,因爲這些標籤不是ruby語法。

,在標籤內,做串插,正常使用Ruby方法:

<%= link_to "Edit", edit_post_path(post.id), :class => "btn", :remote => true, "data-toggle" => "modal", "data-target" => "#{post.id}-my-modal" %> 
0

唯一的錯誤是把另一個(紅寶石)標籤<%%>相同的標籤內。

ERB(嵌入紅寶石)一個<%=%>標記不能有另一個標籤內。

此代碼導致語法錯誤。

<%= link_to "Edit", .... "data-target" => "<%= post.id %>-my-modal" %> 

如果您打算輸出/渲染,在標籤內數據,並引述字符串, 使用#{put_data_hare}

輸出,如:

<%= link_to "Edit", .... "data-target" => "#{post.id}-my-modal" %> 

在其他情況下,你可以這樣做:

post.id.to_s + "-my-modal"