我正在使用simple_form_for @customer
创建一个包含该模型中字段的表单,但是,我也有一个表custom_data,其中包含一些字段,名称和值,这些字段是可选的,具体取决于客户以及服务器何时安装。
我已经使用了simple_fields_for 'custom_data'
示例,但总是让我看到像这样的错误
CustomerData的未定义方法`height':0x007ff33f6932c0
完整代码
%div.grid-body.no-border
%div.row
= simple_form_for @customer do |f|
= f.error_notification
.col-md-6.col-sm-6
.form-inputs
= f.input :document_type
= f.input :document_id
= f.input :first_name
= f.input :last_name
= f.input :email
.col-md-6.col-sm-6
.form-inputs
= f.input :phone
= f.input :mobile
= f.input :address1
= f.input :address2
= f.input :age
- if @custom_fields
- simple_fields_for "customer_att" do |n|
.col-md-12.col-sm-12
.form-inputs
- @custom_fields.each do |field|
= custom_field n, field, @customer
custom_field来自帮助者
def custom_field(f, field, customer = nil)
options = {}
options[:required] = field.required
options[:label] = field.name
if customer.customer_data
customer.customer_data.each do |cd|
if field.id.to_i == cd.customer_field_id.to_i
options[:input_html] = { value: cd.value }
end
end
end
name = "#{field.id} #{field.name}".parameterize.underscore.to_sym
capture do
case field.field_type
when :string
f.input name, options.merge({hint: field.hint})
when :select
concat f.label name, options
concat f.select name,
field.data.split(',').collect{|datum| datum.strip},
options.merge({hint: field.hint})
end
end
end
当simple_form_for不使用模型
时,这很好用