views:

78

answers:

3

My back end is my Rails server which sends JSON data to my front end. In the front end, I am using javascript and jQuery which processes my JSON data and displays some of it.
Now, the user can inputs a number depending on the data displayed to it.
So, on the basis of input from user, certain changes are made to JSON data received earlier and send it back to my back-end as properly encoded JSON.

My question is how can I process this JSON data at the server and store the inputs filled by the user?

A: 

Not sure why you'd need to send the data back as JSON since Rails can just handle the form data normally, as thomasfedb says. But if you definitely do need to, you can use the jQuery serializeArray method and then do a quick conversion from array to JSON. See the following:

http://api.jquery.com/serializeArray/#comment-47479466

If you go down this route, you could use the stringify method in JSON2.js to create valid JSON data from your object.

http://www.json.org/js.html


edited:

Sorry, just realised that you've already got that far! Should have read the question properly.

You can use this JSON ruby implementation to parse the JSON data:

JSON.parse(json_data, {:symbolize_names => true})

and then just use the save method from ActiveRecord::Base to save your record.

joecorcoran
@joecardon :- I have some missing links. I have never done this before i.e sending data back to the server in JSON format.I tried your command but got the following error:JSON.parse({"solutions":[{"name":"I"},{"name":"On"}],"features":[{"name":"Composition"}]})gives me error:SyntaxError: compile error(irb):4: odd number list for Hash(irb):4: syntax error, unexpected ':', expecting '}'BUT, the JSON-data is valid...I passed it through http://www.jsonlint.com/ json parser and it returns valid json.
Silver Spoon
You need to pass the JSON data from your form to JSON.parse as a string: JSON.parse('{"key":"value", "another_key":23}', {:symbolize_names => true})
joecorcoran
A: 

Let's assume you've used JQuery.post() to send the data to the server with a dataType of 'JSON'

Then in your controller you can do this:

MyController << ApplicationController

  def my_action
    @data = params[:data] // client data available as a Ruby Hash object

    // Process data into @result (for example)

    respond_to do |format|
      format.json { render :json => @result.to_json }
    end
  end

end

Or you could leave out the render statement above and provide a view (called my_action.json.erb) in which you can format the JSON response.

bjg
@bjg :- I am unable to understand what you are telling. My question is that I process the user input in a certain manner and then I have to send that data back to the server where I can process it again and act accordingly.The data format in which I am sending data to the server is JSON.I am making a jQuery.post call at the client.
Silver Spoon
@Silver Spoon. I think I understood that. What I have shown is the outline server side logic to process your incoming JSON and respond with JSON back to the client. Was there something else you needed here?
bjg