JSON shown instead of HTML for rails view using ember.js - ruby-on-rails

I'm slowly moving my rails website from a traditional rails round-trip-for-each-view application to one that will eventually be single page ember.js based. As part of this migration, I'm doing it in steps and not migrating the whole app in one go to a single page app but section by section at first.
I'm stumped on one problem that seems general. When I use the same controller for JSON views as well as HTML views, pressing back in Chrome occasionally shows me the JSON view instead of the HTML view.
For instance, I have an endpoint /portfolio/13, which is the entry point into one of these section ember.js apps and which causes Ember Data to pull the JSON for Portfolio with the id of #13 over the same endpoint with application/json as the Accept: header. Pressing back after forward navigating to deeper page will get the JSON representation of the page instead of the HTML.
Do I need to force Ember Data to use the format parameter such that the JSON version is at a different URL? If so, how does one do that?
What am I doing wrong?

Try adding this somewhere in your Javascript:
$.ajaxSetup({cache: false});
It should fix the problem. However, something is wrong here, because by default browsers don't cache JSON. Probably, this is because of the wrong content type, that is, Rails is serving JSON as HTML.

Are you having some headers set as "application/json" in your rails app? Could you trace the network response headers and see the content-type: value?
Is the "whole" page displaying as JSON? ( meaning not parsing HTML )
I had this problem once using sinatra and I had a ( very stupid ) :
before do
content_type 'application/json'
end
If you want your browser to display correctly, it has to be 'text/html' for all your HTML pages.
It might be your problem at some places in your rails app. ( But why haven't you had this problem before ember? )

Related

Posting to a web form and catching results from JavaScript code

How would I go about achieving the following
I have some HTML data triggered from an Evernote new note action
I need to pass this HTML string to a website via an http post with form variables
I then need to catch the resulting web page text in a variable to use in my next action.
For clarity the website simply takes some HTML and converts it to markdown passing it back out as the body of the resulting page
Regards in advance
Dan
Sweet! So I've got this working. In my example, text is some html I pulled out of a dummy previous step:
The output is:
Which has the markdown as a key. If you want to tweak other data based on the api params, you can do that as GET params below html
​Let me know if you've got any other questions!

Navigating back after a non-turbolinks request displays raw JSON on chrome

The case (Chrome only)
I open my webapp
Navigate to another page (turbolinks)
Navigate to
another page (no-turbolinks, e.g. form submittion)
Hit 'Back'
Instead of showing the previous route's HTML, i see a raw JSON. Note that this route, along with others, are configured to respond to both HTML and JSON.
I added the following Javascript to my app and that solved it:
Turbolinks.pagesCached(0)
$.ajaxSetup({cache: false})

Rendering a response from a URL to RSS format

I am creating a controller that receives certain parameters from an application, then accesses a hard coded URL. Upon receiving a response from the URL my controller should render this response to RSS format.
In doing this I decided to use XPath to sort of create the xml tags, I then used StringBuilder to append these tags and then rendered the result as text. This is able to show on the browser just how I want it.
However when I try to view the page source it does not show any tags or headers, it just shows it as normal text on a page. I need help with what to do so that the headers and tags can appear in the page source.
I would suggest having your controller send the data as JSON, and then creating a template that renders the JSON as rss2/xml. For best results, make your JSON structure easy to loop over to create the RSS feed by looking at how feeds are organized.
Here's the rss2 spec
make sure that this line is at the top of your file with NO leading spaces
<?xml version="1.0"?>
Also make sure your content is served with "text/xml" as its content-type. In php, one would set this as such:
header('Content-Type: text/xml');
See http://www.electrictoolbox.com/rss-php-content-type/

What is the best way to handle submission of non-utf-8 data in Rails 3 forms?

I am using Rails 3.2.3 and Ruby 1.9.3.
I have a model called called Post that accepts a title and a description.
The front-end of the site receives information submitted through the back-end through an ajax request. When I fill out the form with, let's say
title: foo
content: foobar
and submit it, I am able to view data through the front-end without a problem.
However, whenever I submit non-utf8 data through the form, for example (mind the fancy quotes):
title: foo
content: “foobar”
When I try to render the form I get the following error:
ActionView::Template::Error (incompatible encoding regexp match (UTF-8 regexp with ASCII-8BIT string))
My .js.erb file looks like this:
$("#my_post").html('<%= escape_javascript(render :partial => 'post') %>');
I realize this is an issue with encoding, but I'm not sure how I should handle it the best way. I thought of several options:
Strip out non-utf8 by using the iconv library -- do this via a before_save filter for every single model in my application
Specifying at the top of the js that the document contains utf-8 (not sure this would work)
Using accept-charset="UTF-8" in my form to force the browser to avoid submission of non-utf-8 content.
I'm not even sure these solutions would help and the most efficient way to do this would be.
Thanks!
I suspect that you're not using the form helpers because you mention the question of adding accept-charset="UTF-8" to your form.
The form helpers will add the accept-charset attribute as well as the snowman parameter which together should ensure you get UTF-8 data from the browser.
See the Rails Form helpers guide.
You need to look carefully to see if
You're actually sending non-UTF-8 data to your app, or
You're sending UTF-8 data, but it is not being recognized by Ruby/Rails
To see which it is, you need to examine the data on the "wire." (What's being sent on the Internet.) Use a peeking tool such as Wireshark or a proxy spy such as Fiddler
Curly quotes can be sent using 8859 or UTF-8.
Recommendation You should set the HTML page to be UTF-8. Any Ajax sent from scripts on the page should then also use UTF-8. See http://www.utf8.com/
Added (Re: comment about how Rails sets form's character encoding)
The issue for Ajax character encoding is how was the page's encoding set. A blog post. So be sure to set the page's UTF-8 encoding in your page template.

Rails take base64

In rails i need to take a base64 string, and use it to generate a picture in rails. Now i'm having trouble, because i need to interact with AJAX calls (im strictly working on the server side, another guy is doing that client work) to send pictures. So far i've been taking requests in my application by having data transferred through the url (in the AJAX requests) but now im not sure if it's possible to transfer such a huge string through the url. How could i take in the data (like how could he pass it to me) to generate a picture.
Note: i've been using paperclip for my application so far, but now uploading through the form is not an option, it needs to be in an AJAX call where data is passed in a single call.
You're right, most browsers limit the length of a URL. The limit on IE8/9 is 2083 characters. Even if your particular browser has a higher limit, many servers limit the URL length as well (apache's default limit is right around 8k). It would be best to submit the image as a POST request with the data in the POST body.
I would use jQuery to POST JSON data to the server. In the controller, if this is set up correctly, you won't have to do a thing to parse the JSON. ActiveSupport will recognize the content type and parse it out into the params hash automatically.
Actually posting the data will depend on which javascript library you're using. Here's an example in jQuery, which you'd probably want to wire up to the onclick event of a submit button. This assumes you have a named route called process_image. This code would go in your view.
$.post(<%= process_image_path %>, { b64_img: "your_base64_image_data" });
In your controller, you can access the posted data with params[:b64_img]. If you want to return something from the controller back to the client, you can do this in the controller:
render :json => #model_object
And change the jquery call to look like this so you can do something with the return value:
$.post(<%= process_image_path %>, { b64_img: "your_base64_image_data" },
function(data) {
// do something with the data returned by the controller
});
Hope this helps. You can read more about the jQuery post call I used here: http://api.jquery.com/jQuery.post/
Dan

Resources