Rails simple_form与嵌套表单的关联

时间:2011-07-28 10:33:36

标签: ruby-on-rails nested-forms simple-form

我的申请有3个模型:顾问,项目和预约 我正在使用带有simple_form gem的嵌套表单

class Consultant < ActiveRecord::Base
  has_many :appointments
end

class Project < ActiveRecord::Base
  has_many :appointments, :dependent => :destroy
  accepts_nested_attributes_for :appointments, :allow_destroy => true
end

class Appointment < ActiveRecord::Base
  belongs_to :consultant
  belongs_to :project
end

我的表格如下:

= simple_nested_form_for(@project) do |f| 

  %div.field
    = f.input :name, :label => 'Nom du projet'
    = f.simple_fields_for :appointments do |builder|
      = render 'appointment_fields', :f => builder
    = f.link_to_add "ajouter un consultant", :appointments

  %div   
  %div.actions
    = f.submit

with partial:

%p.fields
  = f.input :consultant_id, :input_html => { :class => 'special' }
  = f.association :consultant
  = f.input :nb_days, :input_html => { :class => 'special',:size => 10  }
  = f.input :rate, :input_html => {:size => 10}
  = f.link_to_remove "Remove this task"

是否可以使用simple_form做一些简单的事情? 答案是肯定的:它运作良好

1 个答案:

答案 0 :(得分:2)

错误是因为模型Appointment上没有名为顾问的关联。请改用顾问。