Question

I'm trying to use the simple form gem to populate a select box using an association. I have two associated models: job_category and job_type.

job_category has_many job_types

and

job_type belongs to job_category.

I'm successfully populating the select box using this form,

= simple_form_for(@job_type) do |f|  
  = f.error_notification

  .form-inputs  
    = f.input :name  
    = f.association :job_category, :include_blank => false, :label_method => :name  
  .form-actions  
    = f.button :submit  

and it is creating a job_type object when I hit my create route, but the job_category_id field in job_type isn't being set.

When I go to the command line instead, I am able to successfully create a new job using

JobType.create(name: "Test", job_category_id: 1)

Inspecting the post using the Chrome developer tools shows job_category_id being sent over as part of the post, so I don't have any idea why this isn't working.

Was it helpful?

Solution

If you are using Rails 4, can you confirm that you've added :job_category_id to your job_type_params method in JobTypesController?

If you have not, make sure :job_category_id is in your job_type_params method.

If you're using < Rails 4, add :job_category_id to your attr_accessible.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top