0

我正在尝试在任何操作处理之前预处理paramswithto_ar2en_i函数,我在我的 中有以下内容:ApplicationControllerparamsapplication_controller.rb

# translates every params' entity from arabic to english 
before_action :param_convert_ar2en_i

private

def param_convert_ar2en_i h = nil, path = []
  h ||= params
  h.each_pair do |k, v|
    if v.respond_to?(:key?)
      param_convert_ar2en_i v, [path, k].flatten
    else
      # something like: 
      params[[path, k].flatten].to_ar2en_i 
    end
  end
end

问题是我不知道如何应用to_ar2en_i到嵌套params路径为[[path, k].flatten].

有人可以帮我吗?

4

1 回答 1

0

傻我!!而不是试图访问我需要做paramsparams[[path, k].flatten].to_ar2en_i只是访问h[k].to_ar2en_i,并且由于h在 Ruby 中是通过引用传递的,所以它会完成这项工作。

def param_convert_ar2en_i h = nil, l = [], depth: 0
  h ||= params
  h.each_pair do |k, v|
    if v.respond_to? :key?
      param_convert_ar2en_i v, [l, k].flatten, depth: depth + 1
    else
      if h[k].respond_to? :each
        h[k].each { |i| i.to_ar2en_i if i.respond_to? :to_ar2en_i }
      else
        h[k].to_ar2en_i if h[k].respond_to? :to_ar2en_i 
      end
    end
  end
end
于 2017-05-11T07:48:58.917 回答