Search code examples
ruby-on-railssimple-formnested-attributes

simple_form has_many through unpermitted parameters


I'm having a little trouble setting up my simple for for create/update. Here's the form:

<%= simple_form_for( setup_menu(@menu) ) do |f| %>

<%= f.input :user_id, :as => :hidden %>
<%= f.input :name %>
<%= f.input :description %>

<%= f.association :recipes %>

<%= f.button :submit %>
<% end %>

Here are the three models:

class Menu < ActiveRecord::Base
  has_many :courses
  has_many :recipes, through: :courses

  accepts_nested_attributes_for :recipes,
    reject_if: :all_blank

  accepts_nested_attributes_for :courses,
    reject_if: :all_blank
end
class Recipe < ActiveRecord::Base
  has_many :courses
  has_many :menus, through: :courses

  accepts_nested_attributes_for :menus 
end
class Course < ActiveRecord::Base
    belongs_to  :menu
    belongs_to  :recipe
end

And here is the params definition from the menus_controller:

    def menu_params
      params.require(:menu).permit(:user_id, :name, :description, :recipe_ids, :course_ids, :courses, :recipes,
        recipes_attributes: [:id, :recipe_id, :_destroy],     courses_attributes: [:id, :recipe_id, :menu_id, :_destroy]
      )
    end

I keep getting an unpermitted parameter error on the recipe_ids.

Started PATCH "/menus/1" for ::1 at 2015-10-01 20:54:56 -0500
Processing by MenusController#update as HTML
  Parameters: {"utf8"=>"√", "authenticity_token"=>"i/7E27BWul9trszURDu1z8PtsHaG54byG9JOlEp3mn2oQZVveM2mSP4XTLzjWKAK+BNvwoi/pqBbQPrPTcnaDw==", "menu"=>{"name"=>"Menu 1", "description"=>"First test menu", "recipe_ids"=>["", "1", "3", "4"]}, "
commit"=>"Update Menu", "id"=>"1"}
  Menu Load (1.0ms)  SELECT  "menus".* FROM "menus" WHERE "menus"."id" = ? LIMIT 1  [["id", 1]]
Unpermitted parameter: recipe_ids
   (1.0ms)  begin transaction
   (0.0ms)  commit transaction
Redirected to http://localhost:3000/menus/1
Completed 302 Found in 11ms (ActiveRecord: 2.0ms)

I'm sure it is something simple I've missed. I have been scratching my head for a whole day on this. Can someone help?


Solution

  • I knew it was something simple. This takes care of the whole problem.

    params.require(:menu).permit(:user_id, :name, :description, { :recipe_ids => [] } )