Search code examples
ruby-on-railssimple-formreform

Rails 4 with Reform GEM and simple_form


I am trying to implement a nested for using the reform gem. I have three models:

  1. Customer (belongs_to :personal_details)
  2. PersonalDetail (belongs_to :title)
  3. Title (has_many :PersonalDetail)

I am trying to create a CustomerForm using the reform gem using the following code:

/app/forms/customer_form.rb
class CustomerForm < Reform::Form
property      :national_id
validates     :national_id,
            :presence => true

property :personal do

property      :first_name
property      :title


validates     :first_name,
              :presence => true


end
end


/app/controllers/customers_controller.rb
def new
  @form = CustomerForm.new(Customer.new(personal: Personal.new))
end


/app/views/customers/new.html.haml
= simple_form_for @form, :url => customers_path do |f|
  = f.input         :national_id
  = f.simple_fields_for :personal do |builder|
    = builder.input         :first_name
    = builder.association       :title
  = f.button :submit, "Submit"

The :title attribute will not receive any input during a form submission. The relationship between Title and PersonalDetail, is only used to populate a dropdown with a list of Title.name. If I try the above, I get an error message saying that no association :gender exists. When I try something similar using the standard Model based approach it works like a charm.

Any ideas? I have gone through the documentation a few times but simply can't see where I'm going wrong.


Solution

  • I have found a different way to do this. Instead of using simple_form's standard association method, I have simply added a collection to the Select Dropdown like this:

    .row.padding-12-all
      = builder.input :title_id, 
                      :label_method => :name , 
                      :value_method => :id,
                      :collection => Title.all