2

我的应用程序有 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

部分:

%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 做这么简单的事情?答案是肯定的:效果很好

4

1 回答 1

2

错误是因为模型约会上没有称为顾问的关联。改用顾问。

于 2011-07-28T13:48:20.193 回答