ruby on rails - undefined method `[]' for nil:NilClass in nested form using check_box_tag to create associated records -


I get this error when I try to add nested attributes via the check box:

  Undefined method `[] 'for zero: NilClass  

The error is in the controller on this line:" @food_rule = @ meal.food_rules.build (parameter [: Food_rule] [: restriction_ID]) "

my model uses I_many

  class food < ActiveRecord :: Base is has_many: food_rules, dependent: deleted has_many: restrictions ,: through = & gt; : Food_rules, accepts_nested_attributes_for: food_rules, allow_destroy: True End Square FoodRule & lt; ActiveRecord :: Base is_to: Food belongs_to: Restriction end class restriction & lt; ActiveRecord :: Base is_mount: Food_Strol, Dependent :: has has_many: Diet, via = & gt; : Food_rules, accepts_nested_attributes_for: food_rules, allow_destroy: true end  

This is my MealsController my "add_restrictions" method that says one: record meal_restrictions via check box

  def add_restrictions @diet = restriction.k1.order (: name) @meal = Meal.find (parameter [: meter]) parameter [: restriction] [: restriction_id] || = [] @food_rule = @ meal.food_rule.build (Params [: food_rules] [: restriction_ID]) response_to do | Format | If @ food_rules.save format.html {redirect_to t1s3_path (: m = & gt; @ meal.id), notice: 'success! You added food restrictions! '} Format.json {} else format.html {render: new, notice:' oops .... try again '} format.json {json: @ template.errors, status :: unprocessable_entity} End End  

What is being presented

  {"UTF8" => "✓", "_method" = & gt; "Patch", "authenticity_token" = & gt; "BVZ4dhVJKTkI43TQGqESINNwkBSswh3h75ofbRBlTwQ =", "restriction" => {"Ban_id" = & gt; ["," 1 "," "," "," "," "," 7 "," "," "]}," comet This is my form:  
  & lt ;% = Form_for @  

food, url: add_restrictions_path (: m = & gt; @ meal.id) | F | & Gt%; & Lt; Div class = "row" & gt; & Lt; Div class = "form" & gt; & Lt; Div class = "form-group" & gt; & Lt;% @ diet.each | Restriction & Gt%; & Lt;% = hidden_field_tag ​​"food [ban_id] []", zero%> & Lt;% = check_box_tag "Food [ban_id] []", ban.id, @ food. Add layouts_documents? (Restriction.ID)% & gt; & Lt;% = label_tag dom_id (restriction), restriction.name% & gt; & Lt; Br> & Lt ;! - Checkbox - & gt; & Lt;% end% & gt; & Lt; / Div & gt; & Lt ;! - Form Groups - & gt; & Lt;% = f.submit 'Next'% & gt; & Lt;% end% & gt; & Lt; / Div & gt; & Lt; / Div & gt;

edit: progress

  I change @food_rule = @ meal.food_rule.build (parameter [: food_rules] [ : Restriction_ids]) @food_rule to = @ meal.food_rule.build (parameter [: food_rule] [: restriction_ids]), (removed one), but it is still throwing the same error.  

I changed the rules of food to the model .... because it was confusing in understanding this problem with the name ... Thus ... any comment Is in: food_range should be read: food_rules


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -