如何为rails中的序列化字段创建使用自定义变量的表单?

时间:2010-05-14 14:48:32

标签: ruby-on-rails forms serialization

MODEL:

class Listing < ActiveRecord::Base
  serialize :details
  validates_presence_of :name, :details
end

查看:

<% form_for(@listing) do |f| %>
  <div class="text_field">
    <%= f.label :name, "Name" %><br />
    <%= f.text_field :name, :autocomplete => "off" %>  
  </div>
  <!-- CUSTOM DETAILS HERE! -->
<% end %>

1 个答案:

答案 0 :(得分:0)

您可以像这样使用fields_for:

<% fields_for "listing[details]", listing.details do |detail_fields| -%>
  <div class="text_field">
    <%= detail_fields.label :phone_number, "Phone Number" %><br />
    <%= detail_fields.text_field :phone_number %>  
  </div>
  <!-- MORE CUSTOM FIELDS HERE -->
<% end -%>