2014-04-07 81 views
0

我嘗試使用下面的視圖文件助手不顯示HTML內容的使用[Ruby on Rails的]

<%= form_for :spec do |form| %> 
<fieldset> 
<legend><%= @title %></legend> 

<%= text_field_for form, "first_name" %> 
<%= text_field_for form, "last_name" %> 
<div class="form_row"> 
<label for="gender">Gender: </label> 
<%= radio_button :spec, :gender, "Male" %> Male 
<%= radio_button :spec, :gender, "Female" %> Female 
<%= radio_button :spec, :gender, "Other" %> Other 
</div> 
<div class="form_row"> 
<label for="birthdate">Birthdate:</label> 
<%= date_select :spec, :birthdate, 
      :start_year => Spec::START_YEAR, 
      :end_year => Time.now.year, 
      :include_blank => true, 
      :order => [:month,:day,:year] %> 
</div> 
<%= text_field_for form, "occupation" %> 
<%= text_field_for form, "city" %> 
<%= text_field_for form, "state" %> 
<%= text_field_for form, "zip_code", Spec::ZIP_CODE_LENGTH %> 

<%= submit_tag "Update", :class => "submit" %> 
</fieldset> 


<%end%> 

創建一個表單,我使用下列helper方法

def text_field_for (form, field, 
      size=HTML_TEXT_FIELD_SIZE, 
      maxlength=DB_STRING_MAX_LENGTH) 
    label = content_tag("label","#{field.humanize}:", :for => field) 
    form_field = form.text_field field, :size => size, :maxlength => maxlength 
    content_tag("div", "#{label} #{form_field}", :class => "form_row") 
end 

然而,我的輸出給出

<label for="first_name">First name:</label> <input id="spec_first_name" maxlength="255" name="spec[first_name]" size="15" type="text" /> 
<label for="last_name">Last name:</label> <input id="spec_last_name" maxlength="255" name="spec[last_name]" size="15" type="text" /> 

文本框,而不是HTML代碼和標籤

任何人都可以爲此提出解決方案嗎?

回答

1

更新text_field_for方法如下:

def text_field_for (form, field, 
      size=HTML_TEXT_FIELD_SIZE, 
      maxlength=DB_STRING_MAX_LENGTH) 
    label = content_tag("label","#{field.humanize}:", :for => field) 
    form_field = form.text_field field, :size => size, :maxlength => maxlength 
    content_tag "div", label + form_field, :class => "form_row" 
end 

您進行治療labelform_field爲String。你不應該插入它們。

+0

看上去相同:/ – Krishna

+0

您是否照原樣複製?它既可以作爲塊,也可以當前編輯的答案。 –

+0

我將輔助程序更新到了控制器特定的幫助程序文件中,而不是在常用的應用程序幫助程序中。有效!謝謝 – Krishna