开发者

Ruby on Rails - Adding variable to params[]

开发者 https://www.devze.com 2022-12-30 13:12 出处:网络
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

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开发者_如何学Python 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.


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消