开发者

how to get params[:userform] values?

I have params[:userform] array which contains my form elements, I looking for solution like php: arrayname['city开发者_StackOverflow中文版']. I want put elements to variables


Using 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
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜