我正在解析JSON並將其傳遞爲fields_array
以呈現erb
模板。這是一個Sinatra應用程序。如何根據特定標籤獲得參數值?
我:
private
def fields_params
# example of parsed JSON, Company Name sometimes is Field6 but sometimes Field3
[["Company Name", "Field6"], ["Email", "Field5"]]
end
def company_name
# I want to return company name from params[company_field_id]
# Maybe something like:
id = fields_params.select{|field| field[0] == "Company Name" }.flatten[1]
params[id]
end
def fields_array
fields_params.collect do |label, param_id|
{ label: label, value: params[param_id] } if params[param_id]
end
end
我應該如何得到params
company_name
?