2015-06-14 63 views
0

如何將我寫強PARAMS這個JSON,如果我有地圖模型,其中我想保存這些記錄:如何寫字符串params用於在JSON在軌道4,5

{ 
"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) 
+0

您的'JSON params'看起來不對。你確定這些是正確的嗎? – Pavan

+0

@Pavan,我在JSONlint.com檢查 – yerassyl

+0

嘗試'params.permit({map:[:lat,:lon,:vibration_level,:time_sent]})' – Pavan

回答

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) 
+0

我已經有了。 – yerassyl

+0

請在permit params方法中的permit語句之前添加'puts「#{params.inspect}」'併發布params值。 –