3

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

If I try this I get an error: params[:group_] + variable

How should it be done?


Edit per request

Ok, I have a form that sets groups of radio buttons with names like this:

group_01DRN0

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

Radio button "group_01DRN0" could have value of "21" or "22" or "23", radio button "group_01AAI0" could have value of "21" or "22" or "23", etc.

In the DB I have every code (01DRN0, 01AAI0, 01AUI0, etc) so I want to select them from DB and iterate in the params value so I can get the radio button group value, I've tried this with no luck:

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

Thanks.

1
  • Your example code is over-writing @all with each loop iteration... Commented May 14, 2010 at 18:06

2 Answers 2

4
p = params
p[:new_param_name] = new_param_value

It works for me (rails 3.2). Nota: using p instead of altering params avoids altering original parameters.

Sign up to request clarification or add additional context in comments.

Comments

3

params looks like a hash, but it really isn't. So if you need to "augment" params as you deal with the incoming data in your controller, invent a new data structure that includes either params or its members.

Added:

Maybe you're looking for

@codes=Code.get_codes
@all = []    
for c in @codes
  @all << params["group_#{c.name}"]
end

1 Comment

I did not know you could use "group_#{c.name}" inside params[]! But that did the trick, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.