params[:userform]
array which contains my form elements, I looking for solution like php: arrayname['city开发者_StackOverflow中文版']
. I want put elements to variablesUsing an array is a bad idea because you have no idea, inherently, what the values mean since you only know an index; you should use a hash for this very reason.
That being said, to read the values from your array you could do:
params[:userform].each_with_index {|value, index| puts "[#{index}] == #{value}" }
To directly assign your variables you could do this:
some_variable = params[:userform][0]
some_other_variable = params[:userform][1]
Again, you should really use a hash so you can do something like this:
user_name = params[:userform][:name]
user_state = params[:userform][:state]
Much easier to read, much easier to understand, but most importantly it's much easier to turn into metacode:
params[:userform].each do |key, value|
instance_variable_set("@user_#{key}", value)
end
精彩评论