0
我想遍歷提交的參數。 Params陣列可以達到三級。目前我只能像這樣迭代第一級。Ruby on Rails:通過嵌套參數迭代
params[:job].each do |v, k|
#do something with k value, only if it's string. If it's array then find it's string value.
end
當你不知道你在期待什麼時,如何迭代params?
更新答:
def nested_params(nested_hash={})
nested_hash.each_pair do |k,v|
case v
when String, Fixnum then
dosomething(v)
when Hash then nested_params(v)
else raise ArgumentError, "Unhandled type #{v.class}"
end
end
end
謝謝。我已經修好了。它是相似的。 – Sharj
因爲'Module。==='是'Object.is_a?'的一個反向(因爲'obj.is_a?klass'與'klass === obj'相同,所以可以使用'case'(它使用' ===')爲你的'if'的更漂亮的版本:'案例v;當數組然後...;當字符串然後...;結束' – Amadan