2013-06-21 28 views
2

屬性我有一個看起來像這樣的屬性:串插在ERB文件

data-text = "I won ### by playing..." 

,其中###應該是我在@credits值。

我該如何放置那裏?在這種情況下

data-url = <%= request.scheme %> + "//" + <%= request.port %> 

我不知道如何把應該圍繞整個方案+端口「」:

時試圖做我有同樣的麻煩。

感謝

回答

8
data-text = "I won #{@credits} by playing..." 

可以採用同樣的原則爲另一個字符串,像這樣:

data-url = "#{request.scheme}//#{request.port}" 
+0

只是注意'數據url'不是有效的變量名。當你嘗試給它分配任何東西時,你會得到以下結果:'未定義的局部變量或方法數據爲main:Object' – andy

+0

@andy,如果我們不在'html'環境中,那麼'data-url'已驗證。 –

+0

我想你的意思是,'data-url =「<%= request.scheme%> // <%= request.port%>」'因爲它是erb。 –

5

當你有一個ERB文件時,"裏面只有<% %>標籤與Ruby的交互。在他們之外,它被Ruby忽略。

<html> 
    <tag data-text = "I won <%= @credits %> by playing..." 
     data-url = "<%= request.scheme %>//<%= request.port %>"></tag> 
</html> 

會產生,這取決於你的價值觀爲這些變量:

<html> 
    <tag data-text = "I won 42 by playing..." 
     data-url = "example//3000"></tag> 
</html> 
1

我知道查爾斯的解決方案的工作,因爲我之前使用的,和路易斯的解決方案也可能有效,雖然我自己沒有使用那種風​​格之前。

但是,有一個更簡潔的解決方案,而不是醜陋的硬編碼html代碼。他們是標籤助手和鏈接助手。

看他們如何幹淨的處理時,數據屬性:

link_to "homepage", root_path, data: { text: "I won #{@credit}" } 

content_tag "div", data: {url: "#{request.scheme}//#{request.port}"}