I am creating a nested model in Rails, but I want to add fields to nested models in the controller. I do not use hidden_field_tag ββas it can be changed.
Here is my options hash:
Parameters: {"dummy"=>{"users_attributes"=>{"0"=>{"email"=>" jjjj@gmail.com ", "id"=>"", "_destroy"=>"false"}, "1"=>{"email"=>" qqq@gmail.com ", "id"=>"", "_destroy"=>"false"}}}, "commit"=>"Create Dummy"}
I want there to be a field under each user_attributes called companyid. Say I wanted companyid to be a "company", then I thought it would work:
len = params["dummy"]["users_attributes"].size counter = 0 while counter < len params["dummy"]["users_attributes"][counter]["companyid" => "company"] counter = counter + 1 end
But I get the 'undefined method` []' for the nil: NilClass error for the first line in the while loop. I'm not quite sure why.
Can someone help me so that I can change the params hash?
EDIT: So, I finally figured it out. I definitely did not use any solutions. First, I set the hidden_field tag to be empty for companyid. Then in the controller I put:
params["dummy"]["users_attributes"].each do |key, val| params["dummy"]["users_attributes"][key]["companyid"] = "company" end
Not the most elegant code, but it will work.
source share