如何在rails 4中为JSON编写字符串参数

时间:2015-06-14 16:55:03

标签: ruby-on-rails json

如果我想要保存这些记录的Map模型,我将如何为此JSON编写强params:

{
"map":[
  {"lat": "51.088672", "lon": "71.396522", "vibration_level": "300", "time_sent": "07:25:00"},
  {"lat": "51.088672", "lon": "71.396453", "vibration_level": "300", "time_sent": "07:25:01"},
  {"lat": "51.088829", "lon": "71.396476", "vibration_level": "300", "time_sent": "07:25:14"} 
 ]
}

我已经尝试过类似的东西了:

params.require(:map).permit(:lat, :lon, :vibration_level, :time_sent)

params.permit(map: [:lat, :lon, :vibration_level, :time_sent])

params.permit(map: [:lat, :lon, :vibration_level, :time_sent]).require(:maps)

1 个答案:

答案 0 :(得分:0)

试试这个我希望这会有所帮助。

before_action :parse_json_data_to_params, except: [ # Put Your Get Actions Names Here otherwise it will raise error for get requests ]

def parse_json_data_to_params
  params.merge!(ActiveSupport::JSON.decode(request.raw_post))
end

和params许可方法。

params.require(:map).permit(:lat, :lon, :vibration_level, :time_sent)