Using JSON with Michael hartl tutorial? - ruby-on-rails

I would like to save a JSON file in the JavaScript folder and read data from it and save the data to the database. How do I read a JSON file in Ruby on Rails 4?

You should do a respond_to block in your controller, return JSON data and parse it with JavaScript in your view.
If you need to view the straight JSON data just add ".json" (www.your_url.com.json) to the end of the url in your browser and you'll see the data returned, in which case you can copy it and save it wherever you like.

Related

Take a string representation of JSON and render it as JSON in the broswer

I'm playing around with the Twitter api, and have gotten back super long JSON response. I saved the response as a string in a seperate file, and I want to have Chrome display that string as JSON, so I can collapse/ expand the nested parts in JSON view.
I feel like there should be an easier way to do this rather than temporarily changing my api controller in Rails...any suggestions? This is for a Rails 4 app using Backbone.js in the front end.
Ah, stupid mistake on my part -- I was using one of the referred to chrome extensions, JSONView, and asked this question after being surprised that it wasn't working.
The reason it wasn't working was because contents of the file were not actually in JSON format, they were in a ruby hash.
I was able to fix it by replacing this:
File.open('exampleResponse', 'w') do |file|
file.write(Twitter::SearchResults.new(request).attrs)
end
with this:
File.open('exampleResponse', 'w') do |file|
file.write(Twitter::SearchResults.new(request).attrs.to_json)
end
There are many chrome extensions to view formatted json. I use JsonView and it works fine, but I imagine there are dozens if not hundreds to choose from.

How to accept post request via json on rails?

I have a controller which accepts both html and json format data. I want to know how to accept the data of json format from the POST request from the browser's REST client. Null values are getting stored in the database. How to convert Json format data into string to store it in the database? I have used rabl gem to filter json contents. Ruby on Rails.
You might want to take a look at the serialize class method:
http://apidock.com/rails/ActiveRecord/Base/serialize/class
This allows one to easily save the incoming JSON format and parse it to a Hash when retreiving it from the database.

Rails display data from XML file via respond_to format.json

I have an XML file containing text data that I need to display to the user. I am using ember.js and therefore need to provide my response in json format.
My initial thoughts are to load the XML file and convert it to json using XSLT and then rendering this. However, I don't fully understand how the respond_to format.json method works. At a guess, I would say it turns the result of the instance variable into json, so if my data is already in json would it cause me any issues using this approach?
What is the best way to render my XML file to a view in json?
If you want to combine json and xml features, you might use rabl:
https://github.com/nesquena/rabl

ruby on rails: Convert an image file to byte array

I need to upload an image from a rails form using Ajax and convert it into a byte array to show a html preview of the image.
When I read the file, it returns me binary data which is not readable by img tag. I'm sure I'm doing something very silly and this might have an obvious solution. Here is the code snippet. Please help.
rails
tmp = File.open(params[:file_upload][:my_file].tempfile, 'rb').read
render :text => tmp
jquery
$("#item_detail_image").attr("src", "data:image/png;base64," + data.responseText);
I'm not using paperclip because I don't have a database connection to my rails application (I'm using web services) and I'm not sure how to use paperclip without ActiveRecord
You need to Base 64 encode the data.
See http://www.ruby-doc.org/stdlib-1.9.3/libdoc/base64/rdoc/Base64.html#method-i-encode64

Using restclient with multipart posts

I'm using restclient for a multipart form to send data to a restful web service (it's Panda video encoding service).
The trick though, is that the file I am passing into restclient (Technoweenie branch) is coming from my own form that a user submits.
So, lets walk through this. A user posts a file to my rails app. In my controller, it receives the file from params[:file]. I then want to pass params[:file] down to Panda using RestClient.
The error I'm getting is on the Panda server follows. I noticed that the file param in the stack trace is in a string as well (which I assume is Panda turning into a string for a nicer stacktrace).
~ Started request handling: Wed Aug 12 18:05:15 +0000 2009
~ Params: {"format"=>"html", "multipart"=>"true", "account_key"=>"SECURE_KEY", "action"=>"upload", "id"=>"SECURE_ID", "controller"=>"videos", "file"=>"#<File:0xcf02ca4>"}
~ 9bfb1750-6998-012c-4509-12313900b0f6: (500 returned to client) InternalServerErrorcan't convert nil into String
/var/local/www/panda/app/models/video.rb:246:in `extname'
/var/local/www/panda/app/models/video.rb:246:in `initial_processing'
/var/local/www/panda/app/controllers/videos.rb:79:in `upload'
I doubt you can really pass a CGI-style upload param from Rails into restclient and expect it to work.
A regular upload in Rails would have quite some extra attributes which do not belong in a posted resource (like the original filename and so on), and a Rails upload contains an IO with the actual file data. Also a file upload object in Rails might be a Tempfile handle and might be a StringIO - depending on the size of the upload.
What you effectively need to do is "repackage" your upload for rest-client to handle it properly, and pass the repackaged and rewound Tempfile object to restclient. Maybe you can get away with just picking the upload object itself instead of the whole params[:file]
Confirm that your restclient action can save locally first. If the action cannot save locally, then you will have a better idea where to look while trouble shooting.
Looks like the problem is with rest-client's posting of the file, check out an alternative method for posting like curb.
Lots of examples for posting multipart form data on this question: Ruby: How to post a file via HTTP as multipart/form-data?

Resources