控制器功能接收参数,如
{"v1" => { "v2" => "1", "v3" => "" }, "v4" => "true"}
params
功能允许使用
x = params[:v1]
,相当于x = params["v1"]
if params[:v4]
,相当于["true", "1"].include?(params["v4"])
if (params[:v1][:v2] == 1)
,相当于params["v1"]["v2"] == "1"
是否有任何方法可以使用params
函数而不是其他数据?
我希望能够写出类似的东西......
my_params = {"v1" => { "v2" => "1", "v3" => "" }, "v4" => "true"}
x = my_params[:v1]
if my_params[:v4]
if (my_params[:v1][:v2] == 1)
或使用函数some_function
x = some_function(my_params)[:v1]
if some_function(my_params)[:v4]
if some_function(my_params)[:v1][:v2] == 1)
我在Rails 2中。
答案 0 :(得分:3)
您需要hash with indifferent access:
h = { a: { b: 1, 'c' => 2 } }
=> {:a=>{:b=>1, "c"=>2}}
h[:a][:c]
=> nil
h2 = h.with_indifferent_access
=> {"a"=>{"b"=>1, "c"=>2}}
h2['a'][:c]
=> 2
h2[:a][:c]
=> 2