Changing params hash in Rails

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.

+6
source share
3 answers

The hash parameter is special because (especially with forms) it maps one-on-one to the model (s) with which it is associated, and Rails expects it to be either a database column or a method in the model with that name.

Hidden fields are a typical solution to the problem of obtaining additional data, but if you do not want users to see these fields, you are faced with the problem that HTTP does not have a status. Therefore, in this case, session[:something] is your friend. Before displaying a form or page that may have hidden data, instead add data to the session variable (first encrypt it), after which you can read (and decrypt) when the user submits the form. Or you can save it in the database table and put only the row identifier in the session so that the data can be read after the form is submitted.

If the data was part of the model, but you simply did not want to display it, you could simply display the parts that the user could see in the view, but then see the rest in the controller action when the User submitted a form or request.

If the data is insensitive, you can simply declare the values ​​that the user can change as attr_accessible in their respective models (prevents mass assignment) and put the rest in hidden fields.

+4
source

It looks ugly, and I don't understand what is happening, but your mistake is:

 len = params["dummy"]["users_attributes"].size counter = 0 while counter < len params["dummy"]["users_attributes"][counter] = { "companyid" => "company" } counter = counter + 1 end 

I believe you should use accepts_nested_attributes_for here and initialize new companies in the controller, not this ugly code

http://railscasts.com/episodes/196-nested-model-form-part-1

http://railscasts.com/episodes/197-nested-model-form-part-2

0
source

This is not how hashes work. Try the following:

 params["dummy"]["users_attributes"][counter]["companyid"] = "company" 

EDIT In addition to fl00r's answer, yes, you should set ["companyid"] as {} first.

0
source

Source: https://habr.com/ru/post/911234/


All Articles