2014-07-13 42 views
1

在rails標籤中,連字屬性的語法是什麼?例如data-something: "1234"是無效的語法。Rails中帶連字符的html標籤的屬性的語法

<%= link_to "foo", "/bar", class: "myClass", id: "myID", data-something: "1234" %> 
+0

你是什麼意思的'連字屬性'? – Pavan

+0

沒關係,你得到了有效的答案。 – Pavan

回答

1

嘗試:

<%= link_to "foo", "/bar", class: "myClass", id: "myID", data: { something: "1234"} %> 
+0

感謝Mandeep,這將適用於數據屬性,但總的來說有沒有辦法逃避hypthen?例如,在數據中,如果您有類似「my-attribute」的內容,那麼{{my-attribute:「124」}'無效 –

+0

@DonnyP因爲您需要做數據:{my_attribute:「124」}和它會自動轉換爲data-my-attribute – Mandeep

2

已經有一個答案,但在一般使用「連字符連接屬性」(不知道這是科學術語雖然),是來包裝它這個樣子。 ..

<%= link_to "foo", "/bar", "data-something" => "1234" %>