vendredi 22 mai 2015

Multi-layer nested attributes with check_box_tag

I'm trying to build a complex form for my admin panel. The form is to update a company. Nested within the form is a form to update the job_postings for that company. Then, nested within each job_posting form is a form to select experience_levels for the job posting.

The nested job_posting form works, but I can't get the experience_levels attributes to be nested under the job_postings param.

Here are my models:

class Company < ActiveRecord::Base
  has_many :job_descriptions, dependent: :destroy
  accepts_nested_attributes_for :job_descriptions, allow_destroy: true
end

class JobDescription < ActiveRecord::Base
  belongs_to :company
  has_many :job_description_experience_levels
  has_many :experience_levels, through: :job_description_experience_levels

  accepts_nested_attributes_for :job_description_experience_levels, allow_destroy: true
end

class JobDescriptionExperienceLevel < ActiveRecord::Base
  belongs_to :job_description
  belongs_to :experience_level
end

class ExperienceLevel < ActiveRecord::Base
  has_many :job_description_experience_levels
  has_many :job_descriptions, through: :job_description_experience_levels
end

Here is my form:

<%= form_for @company, url: admin_company_path(@company), method: :patch do |f| %>
    <%= f.fields_for :job_descriptions do |builder| %>
        <fieldset id=<%="job_description_#{f.object.id}"%>>
            <%= f.label "Experience Level" %>
            <%= f.fields_for :experience_levels do |builder2| %>
                <% @experience_levels.each do |level| %>
                    <%= check_box_tag "job_description[experience_level_ids]", level.id, nil %>
                    <%= label_tag level.name %>
                <% end %>
            <% end %>
        </fieldset>
    <% end %>
<% end %>

In my companies_controller I have this method for my params:

def company_params
    params.require(:company).permit(job_descriptions_attributes: [:id, :_destroy, :title, :role, :description, :posting_link, :experience_level_ids] )
end

When I submit the form with one of the experience_levels checked, the params are not nested. They look like this:

{"utf8"=>"✓",
 "_method"=>"patch",
 "authenticity_token"=>"5AIuu8Y/nV0jeIkxLoyXSftwcMJoUbgTLvN/WnFTkdQ=",
 "company"=>
  {"visibility"=>"public",
   "direct_apply"=>"true",
   "name"=>"Ice 2 Go",
   "comments"=>"",
   "short_description"=>"Boston's only jewelry drive-thru",
   "street_address"=>"23 Really Long Name Street",
   "city"=>"Boston",
   "state"=>"MA",
   "hr_first_name"=>"Joe",
   "hr_last_name"=>"Dirt",
   "hr_email"=>"joe.dirt@example.com",
   "companies_industries_attributes"=>{"0"=>{"industry_id"=>"6", "_destroy"=>"0", "id"=>"8"}},
   "size"=>"1-10",
   "brand_color"=>"#53AA2A",
   "who_card_attributes"=>
    {"title"=>"I2G",
     "description"=>
      "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc vulputate mauris ornare purus vehicula, sit amet sodales tortor vestibulum. Phasellus neque mauris, posuere dignissim ante eu, bibendum tincidunt ante. Proin viverra volutpat vestibulum.",
     "facebook_link"=>"http://www.facebook.com",
     "twitter_link"=>"http://www.twitter.com",
     "linkedin_link"=>"http://www.linkedin.com",
     "website"=>"http://website.com",
     "video_attributes"=>{"media_url"=>"//www.youtube.com/embed/sbvqbICyk2A", "media_type"=>"Youtube", "id"=>"12"},
     "photo_attributes"=>{"delete_image"=>"0", "id"=>"38"},
     "id"=>"6"},
   "team_card_attributes"=>{"title"=>"", "rank"=>""},
   "office_card_attributes"=>{"title"=>"", "rank"=>"", "map"=>"0"},
   "job_descriptions_attributes"=>
    {"0"=>{"_destroy"=>"0", "role"=>"Lead Developer", "title"=>"Ruby Ninja", "description"=>"Awesome sauce!", "posting_link"=>"http://www.google.com/", "id"=>"11"}}},
 "single_tags"=>
  {"Hours"=>"Nine-to-Five",
   "Work Style"=>"Cooperative & Collaborative",
   "Pace"=>"Steady & Predictable",
   "Feedback & Mentoring"=>"Informal & On-the-Fly",
   "Training"=>"Learn by Doing",
   "Dress Code"=>"Business Formal",
   "Events & Activities"=>"Every Now & Then",
   "Meals & Snacks"=>"Occassional"},
 "job_description"=>{"experience_level_ids"=>"2"},
 "commit"=>"Update Company",
 "action"=>"update",
 "controller"=>"admin/companies",
 "id"=>"ice-2-go"}

I don't know what I'm missing here. I want the experience_levels attributes to be nested under the job_posting attributes in the params hash. How can I do this?

Thanks!

Aucun commentaire:

Enregistrer un commentaire