Ruby on Rails - adding a variable to parameters []

In the controller, how can I add a variable at the end of params []?

If I try this, I get the error message: params [: group_] + variable

How to do it?


Edit for request

Ok, I have a form that sets up groups of radio buttons with these names:

group_01DRN0

Obviously, I have different groups in the form (group_01AAI0, group_01AUI0, etc.), and the value is set according to the switch selected inside the group:

The "group_01DRN0" camera may have a value of "21" or "22" or "23", the switch "group_01AAI0" may have a value of "21" or "22" or "23", etc.

I have every code in the database (01DRN0, 01AAI0, 01AUI0, etc.), so I want to select them from the database and iterate over the params value so that I can get the value of the switch group, I tried this without luck:

@codes=Code.get_codes
for c in @codes
  @all=params[:group_] + c.name
end

Thank.

+3
source share
2 answers

params looks like a hash, but actually it is not. Therefore, if you need to "increase" the parameters when working with incoming data in the controller, come up with a new data structure that includes either parameters or its members.

Added:

You may be looking for

@codes=Code.get_codes
@all = []    
for c in @codes
  @all << params["group_#{c.name}"]
end
+3
source
p = params
p[:new_param_name] = new_param_value

This works for me (rails 3.2). Note: using pinstead of changing paramsavoids changing the initial parameters.

+4

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


All Articles