2012-11-25 14 views
2

我想實現這個使用HTML:的Rails試圖在f.label標籤

<label ...>First name<em>*</em></label> 
<input ... /> 

與此:

<%= d.label :first_name do %>First Name<em>*</em><% end %> 
<%= d.text_field :first_name, :required => "required" %> 

,但它實際上產生:

First Name 
<em>*</em> 
<label ...> 
    First Name 
    <em>*</em> 
</label> 
<input .../> 

我如何獲得我想要實現的html輸出?

回答

2

嘗試用原料

<%= d.label :first_name, raw("First Name<em>*</em>") %> 
3

只需通過標籤內容作爲第二個參數來將擋,並用.html_safe,以防止它被編碼:

<%= d.label :first_name, "First Name<em>*</em>".html_safe 
2

雖然這兩種回答得完成工作,從網頁設計的角度來看,它並不是真正的「最正確」方式。

相反,考慮給標籤一個html類,如「必需」。然後在CSS中爲「required」類的標籤元素定義一個選擇器,添加一個*並根據需要對其進行設置。

下面是我的一個項目的一些SCSS:

.required::before { 
     content: '*'; 
     color: $orange; 
     margin-right: 2px; 
     position: relative; 
     left: -2px; 
     }; 
+0

這是跨瀏覽器IE7,IE8,IE9,Chrome瀏覽器,FF,Safari瀏覽器兼容? – Catfish

+0

可能嗎?這不是我關心的問題,所以你必須自己測試。這是非常簡單的CSS東西,所以我相信它的工作沒有太多麻煩。 – vpsz