My JavaScript serializes view fields and add waypoints array to post data, in controller each parameter separately add to Geo object encounters a waypoint parameter and it parse to array. Whether can be done a filter that would get these parameter and parse it so that I can use normal approach? Now in normal approach that is Geo.new(params[:geo]) rails returns (Field was defined as a(n) Array, but received a String with the value “[\”Paris, France\”,\”Stuttgart, Deutschland\”]”.).
Controller:
def create
@geo = Geo.new
@geo.waypoints = JSON.parse params[:geo][:waypoints] if params[:geo][:waypoints].is_a? String
@geo.description = params[:geo][:description]
@geo.start = params[:geo][:start]
@geo.end = params[:geo][:end]
@geo.save
redirect_to geo_path(@geo), :notice => "Geo successfully submitted."
end
JS:
$('form').submit(function () {
var waypoints = ["Paris, France", "Stuttgart, Deutschland"]
var url = $(this).attr('action');
var waypoints_temp_field = $('<input type="hidden" name="geo[waypoints]" />').val(JSON.stringify(waypoints)).appendTo(this);
var postdata = $(this).serialize();
$.post(url, postdata, function (callback) {
// callback
}, "json");
return false;
});
Well, couldn’t you change the
waypoints_temp_fieldto post to something other thangeo? For example:Then in your controller, you could have:
This way, you can process waypoints separately from everything else.