我尝试在任何操作处理params
之前预先处理to_ar2en_i
ApplicationController
params
函数,我在application_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]
。
有人可以帮我这个吗?
答案 0 :(得分:0)
params
而不是尝试访问params[[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