Answer the question
In order to leave comments, you need to log in
How to choose the response format depending on what the browser wants?
There is a page localhost:3000/err404 which displays the corresponding simple information. The user should get to this page if the user entered an incorrect address on my site. But this also happens when my js script accesses a route that I inadvertently deleted. And it turns out that I'm waiting for JSON, for example, and the server spits out html. And I don't see a 404 error. I can only see JSON parsing error. How do I decide in the err404 action whether to render the 404.html.erb template or send the standard json for my project {:status=> "error", :message=> "404"}? Well, or in other words, how to find out in what form the browser expects a response?
Answer the question
In order to leave comments, you need to log in
curl local:3000 -Iv
* About to connect() to local port 3000 (#0)
* Trying 127.0.0.1... connected
> HEAD / HTTP/1.1
> User-Agent: curl/7.22.0 (i686-pc- linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
> Host: l:3000
> Accept: */*
In the "Accept" header, the browser passes the MIME types that it wants to accept. True, the format can also be transferred to the url. On this basis, the rail chooses how to respond to it. Everything is simple, no complicated algorithms. But I don't remember exactly how.
In respond_to, it runs through the available formats from top to bottom, trying to return the required one, or, by convention, selects a file.
You can make a test action and experiment. Headers and formats will be available in the debugger.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question