I have the following array of hashes that I send to my controller:
comparisons = [{
"startdate": "01/01/2016",
"enddate": "01/02/2016"
}, {
"startdate": "01/03/2016",
"enddate": "01/04/2016"
}, {
"startdate": "01/05/2016",
"enddate": "01/06/2016"
}, {
"startdate": "01/05/2016",
"enddate": "01/06/2016"
}];
$.ajax({
url: '/get_data',
method: 'GET',
dataType: 'JSON',
data: {
startdate: '01/01/2016',
enddate: '01/01/2016',
comparisons: comparisons
},
success: function (data) {
console.log(data);
}
});
And then in the controller I want to loop through these comparisons:
@comparisons = []
if !params[:comparisons].blank?
params[:comparisons].each do |c|
@comparisons.push({:startdate => c[:startdate], :enddate => c[:enddate], :data => get_data(c[:startdate], c[:enddate], 'week')})
end
end
I get an error: > no implicit conversion of Symbol into Integer
And when debugging I'm finding that the c
in my loop isn't structured the same as what I'm sending...
c: ["0", {"startdate"=>"01/01/2016", "enddate"=>"01/01/2016"}]
How can I fix this?
easiest fix would be to change how you refer to the c
data... you can see that c
is an array (explains the error, it's expecting array elements to be accessed by an integer) and you can see the second (last) element of the c
array has the data you want.
@comparisons.push({:startdate => c.last[:startdate],
:enddate => c.last[:enddate],
:data => get_data(c.last[:startdate], c.last[:enddate], 'week')})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.