2013-02-08 57 views
0

我用以下時不會使任何一個html.erb文件:html.erb不能正常呈現

<% form_for :profile do |form| %> 

時,這是使用(注意「=」號):

<%= form_for :profile do |form| %> 

這是HTML的輸出:

web page

部分的HTML代碼:

<%= form_for :profile do |form| %> 

<%= text_field_for form, "first_name" %> 
<%= text_field_for form, "last_name" %> 
<div class="form_row"> 
    <label for="gender">Gender:</label> 
    <%= radio_button :profile, :gender, "Male" %> Male 
    <%= radio_button :profile, :gender, "Female" %> Female 
</div> 
<div class="form_row"> 
    <label for="birthdate">Birthdate:</label> 
    <%= date_select :profile, :birthdate, 
        :start_year => Profile::START_YEAR, 
        :end_year => Time.now.year, 
        :include_blank => true, 
        :order => [:month, :day, :year] %> 
</div> 

的form_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  

控制器的部分:

def edit 
@user = current_user 
@user.profile ||= Profile.new 
@profile = @user.profile 
if param_posted?(:profile) 
    if @user.profile.update_attributes(params[:profile]) 
    flash[:notice] = "Changes saved." 
    redirect_to :controller => "users", :action => "index" 
    end 
    end 
end 
+0

我相信它應該是'<%= form_for @profile do | form | %>' – Deekor 2013-02-08 23:32:24

+0

已經試過,沒有運氣:( – 2013-02-08 23:35:11

+0

你在你的控制器中是否定義了'@profile'?'@profile = Profile.new' – Deekor 2013-02-08 23:35:47

回答

1

在你text_field_for幫手需要聲明的是,字符串作爲內容傳遞你的DIV由助手生成的標籤是安全的,因此不應該進行消毒。這是通過html_safe完成的。

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}".html_safe, :class => "form_row") 
end 
+0

工作!非常感謝:) – 2013-02-09 00:14:36