开发者

Rails simple_form association with nested form

My application has 3 models : consultant, project and appointment I am using a nested form with 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

My form is as follows :

= 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 the partial :

%p.fields
  = f.input :consultant_id, :input_html => { :class => 'special' }
  = f.association :consultant
  = f.input :nb_days, :input_html => { :class => 'special',:s开发者_如何转开发ize => 10  }
  = f.input :rate, :input_html => {:size => 10}
  = f.link_to_remove "Remove this task"

Is it possible to do something as simple as this with simple_form ? The answer is YES : it works nicely


The errors is because there is no association called consultant on the model Appointment. Use consultants instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜