0

我有一个 html.erb 文件,在使用以下内容时不会呈现任何内容:

<% form_for :profile do |form| %>

使用时(注意“=”符号):

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

这是html的输出:

网页

部分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
4

1 回答 1

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
于 2013-02-09T00:04:36.967 回答