views:

27

answers:

1

Hello, I am currently working on rails3 application that uses jQuery. I have a javascript plugin that returns an array of JSON data based on selections that the user has made. I have a save button that reads this array of data form the java script plug in and posts it back to the rails application to be saved. The javascript seems to be working, it makes the correct ajax call and sends the data back to the rails application. The problem seems to be the way rails is encoding the data that has been PUT back to the server.

The problem is the nested data I am trying to post back to the rails application. I have an array of shifts that seems to be getting treated as one long string and not the hash of values I am expecting. Here is the output of the data rails is getting.

Parameters: {"shifts"=>"[{\"start_time\":\"Wed Oct 06 2010 09:30:00 GMT+0100 (BST)\",\"end_time\":\"Wed Oct 06 2010 13:00:00 GMT+0100 (BST)\",\"active\":true,\"occurrence\":\"weekly\",\"company_id\":\"1\"},{\"start_time\":\"Thu Oct 07 2010 09:30:00 GMT+0100 (BST)\",\"end_time\":\"Thu Oct 07 2010 13:00:00 GMT+0100 (BST)\",\"active\":true,\"occurrence\":\"weekly\",\"company_id\":\"1\"}]", "id"=>"1"}

I would expect to be able to then use the following code in my controller

params[:shifts].each do |shift|
  puts shift.inspect # would expect a hash of values here
end

However instead of the hash of values there is just a single string. Any suggestions as to why rails is not encoding the hash values as you would expect it too.

A: 

I believe you would need to parse it as JSON first. "require 'json'" at the top of your controller then try JSON.parse(params[:shift]).each {|shift| shift.inspect} .

Curtis Edmond
Yep its working now. Thanks.
Stewart