Understanding Rails Controller Request Format: "Processing by MyController#action as FORMAT" - ruby-on-rails

In all likelihood, this question reveals my gross misunderstandings of Rails, HTTP, and the internet.
Problem
I have a request hitting my server from a "transparent redirect" via Stripe API. The request is coming in as HTML format. I want to respond to it in JS format in an AJAX-esque way. My code looks like this:
def create
.... other code ....
render "attendances/create", formats: [:js]
end
I have a view file "attendances/create.js.erb." This successfully sends a response, but rather than the response executing the JS, the JS is instead rendered as HTML. It thus looks like plaintext JS.
Thank you for taking the time to consider this!

What are the request headers, Your ajax request must have accept request headers of javascript instead of default html, for it to allow/execute the js response directly.
See datatype here , it should be script

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!

Json vs html.erb in ruby

My project is on ruby on rails.
I have one of ruby api returns json.jbuilder.
But I want that my api should not return json , it will display html.erb file.
Is anybody can help me???
First of all, it is not recommended to forcefully respond with html template for a json request or vice versa.
I hope you have enough reasons for this which are best known to you.
Anyways, if you want to forcefully process a particular request as html, you can append .html in the end of request url.
e.g. If you want to process index action of users controller whose url is
http://localhost:3000/users
To achieve it
Call controller action with url
http://localhost:3000/users.html, controller will treat request as html and it will respond with html(you will need to have a html template for your controller action).
Similarly if you want to respond json for any request, append .json to the request url,
http://localhost:3000/users.json
controller will treat request as json and it will respond with json(you will need to have a json template for your controller action).

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/

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

AJAX request with Rails 3 and jQuery is being processed as HTML.

I have a form that i want submitting with AJAX, using rails 3 and jquery. The problem that I am facing is that rails is responding to the AJAX request as HTML too. After a little search I found that this has something to do with the correct Accept headers not being passed. How do i fix this?
This is the controller code
respond_to do |format|
format.js { render 'user/create' }
format.html { redirect_to ((params[:feed][:url].nil?)?url_for(:home) : params[:feed][:url]) }
end
It seems to work on a friends firefox, and on my chrome too, smthng wrong with my firefox?
UPDATE: It seems that the error arises only when I use a proxy service as JonDo, which probably changes the accept headers... Is there a way to force rails to use js format if the X-requested-by header is present?
Thanks
Your controller code looks correct. Are you sure that you have added the .js suffix to the AJAX URL in your form? That's how the responder knows what format you want for the response. The default format is HTML so omitting the suffix would look like it's responding to a HTML request.
You likely didn't set the dataType properly. See the docs (this is &.post) If you set dataType to "js" you'll be all good!

Resources