你可以:
- 扫描 FieldStructure 字符串以确定所需字段的 ID。
- 然后使用 id 在 params 哈希中找到所需的值
例子:
params = {"FieldStructure"=>
"{\"Fields\":[{\"Title\":\"Company Name\",\"Instructions\":\"\",\"IsRequired\":\"0\",\"ClassNames\":\"\",\"DefaultVal\":\"\",\"Page\":\"1\",\"Type\":\"text\",\"ID\":\"Field6\"},
{\"Title\":\"Email\",\"Instructions\":\"\",\"IsRequired\":\"0\",\"ClassNames\":\"\",\"DefaultVal\":\"\",\"Page\":\"1\",\"Type\":\"email\",\"ID\":\"Field5\"}]}",
"FormStructure"=>
"{\"Name\":\"TestForm\",\"Description\":\"This is my form. Please fill it out. It's awesome!\",\"RedirectMessage\":\"Great! Thanks for filling out my form!\",\"Url\":\"testform\",\"Email\":null,\"IsPublic\":\"1\",\"Language\":\"english\",\"StartDate\":\"2000-01-01 12:00:00\",\"EndDate\":\"2030-01-01 12:00:00\",\"EntryLimit\":\"0\",\"DateCreated\":\"2013-04-09 06:44:12\",\"DateUpdated\":\"2013-04-10 05:25:55\",\"Hash\":\"z7x3p3\"}",
"Field6"=>"testse",
"Field5"=>"gdsgfdgdf@fd.pl",
"CreatedBy"=>"public",
"DateCreated"=>"2013-04-10 07:38:09",
"EntryId"=>"21",
"IP"=>"46.205.117.55",
"HandshakeKey"=>""}
fields = params['FieldStructure'].scan(/Title\":\"(.+?)\".+?ID\":\"(.+?)\"/)
#=> [["Company Name", "Field6"], ["Email", "Field5"]]
Hash[fields.collect{ |field|
[
field[0].gsub(/\s+/, "_").downcase.to_sym,
params[field[1]]
]
}]
#=> {:company_name=>"testse", :email=>"gdsgfdgdf@fd.pl"}