0

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?

1 Answer 1

1

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')})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.