-1

I can't figure out why this nested form is not working. Skill belongs_to Profile and profile has_many skills. For some reason, the sever logs show the following error when the form is submitted:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"gdskfkjsdflkdjshgk",
 "profile"=>{"skill"=>{"label"=>"PHP"}}, "commit"=>"ADD +", "id"=>"134"}

Unpermitted parameters: skill

Here is the form code:

<%= form_for @profile  do |f| %>            
        <%= f.fields_for :skill do |builder| %>
               <%= builder.select :label, @crafts.collect {|craft| craft.label},
                  {include_blank: true} %>
        <% end %>   

        <%= f.submit "ADD +", :id => "clickme", :class => 'button button-add' %>
<% end %>

Here are the strong parameters in the profiles controller:

params.require(:profile).permit( skill_attributes: [:label, :id])   

Here is the edit controller for the form action:

def edit
    @profile = current_user.profile 
    @skill = Skill.new  #field for new skill
end

Here is the relevant part of the profile model:

class Profile < ActiveRecord::Base
    has_many :skills
    accepts_nested_attributes_for :skills,  :allow_destroy => true
end

The reason I use "skill_attributes" and "f.fields_for :skill" is because I only want one skill to be created/updated at a time and using the plural puts two inputs on the page.

Philip7899
  • 4,599
  • 4
  • 55
  • 114

1 Answers1

0

Your form (skills) doesn't match with what is expected for accepts_nested_attributes (skills_attributes). You can use this I think:

= fields_for "profile[skills_attributes]" do |builder|
John Naegle
  • 8,077
  • 3
  • 38
  • 47