can someone explain to me what respond_to does? (Rails) - ruby-on-rails

So when one generates scaffodl, the controller automatically creates these blocks(?) like this
respond_to do |format|
format.html
format.xml { render :xml => #c }
end
what does this really do and how come it has format.html and format.xml? What does each do?

It defines that the current action will respond to various formats (the action's content can be rendered in many ways, not only plain old HTML).
If you open your browser and type /my/path/to/action.html, it will render HTML (from the template);
If you type /my/path/to/action.xml, it will render XML using { render :xml => #c }. XML will be generated by Rails by calling the to_xml method on the #c variable;
However, if you point to /my/path/to/action.json, it will throw a 404 error.

Rails uses the MIME type of the request determined by the Accept header or format (/controller/action/5.xml; /controller/action/5.html; /controller/action/5.json; etc… ) to determine the response format of the controller action that maps to the requested url.
This way rails can automagically render different content formats for many types of requests to the same controller action.

Related

devise not working when request header contains accept with value image/*;q=0.8

I am using devise in my ruby on rails application.
I am calling following endpoint from a laptop web browser
https://myaddress/users/sign_in
It works well, but when I hit the same endpoint in mobile device it sends the request with header accept as Accept: image/*;q=0.8
Now server responds with 406 not acceptable. How can I force devise gems to accept request which may contain header accept with value image/*;q=0.8
I am not sure why you want to respond to an endpoint requesting an image, but it seems to me that you're looking for respond_to. See also this answer.
I'd guess you want to respond to "any". To support image MIME types, you can find an example here (emphasis mine):
If you need to use a MIME type which isn't supported by default, you
can register your own handlers in config/initializers/mime_types.rb as
follows.
Mime::Type.register "image/jpg", :jpg
respond_to also allows you to specify a common block for different
formats by using any:
def index #people = Person.all
respond_to do |format|
format.html
format.any(:xml, :json) { render request.format.to_sym => #people } end end
In the example above, if the format is xml, it will render:
render xml: #people
Or if the format is json:
render json: #people
any can also be used with no arguments, in which case it will be used
for any format requested by the user:
respond_to do |format| format.html format.any { redirect_to
support_path } end
Formats can have different variants.

Rails Controller - What is the purpose of "respond to do format" html and json?

Ruby on Rails Question:
Inside the controller you have seven REST actions. Almost all of them have respond to do format xml/html or json. I have no idea what this means. Can you please explain it's purpose.
For example:
def index
#tweets = Tweet.all
respond_to do |format|
format.html
format.json { render json: #tweets }
end
end
What is the purpose of the "respond to" part that contains the html and json? What do these formats do?
Also, what is the difference between xml and html? Sometimes I see xml and other times html.
Thank you
Its just a way of telling your controller how to respond to different request types. For example your client could want html or xml information from you:
def index
#people = Person.find(:all)
respond_to do |format|
format.html
format.xml { render :xml => #people.to_xml }
end
end
What that says is, "if the client wants HTML in response to this action, just respond as we would have before, but if the client wants XML, return them the list of people in XML format." (Rails determines the desired response format from the HTTP Accept header submitted by the client.)
http://apidock.com/rails/ActionController/MimeResponds/InstanceMethods/respond_to

What does respond_to do?

#some instance variables
respond_to do |format|
format.html
format.xml {...}
format.json {...}
end
Does respond_to simply send all instance variables to the next webpage or it does something more?
I'm wondering how much data would be sent by respond_to. For example, if I have many instance variables #one #two #three etc. Are they all sent by respond_to? Any other data would be bundled and sent as well? ?
You instance variables will not be sent anywhere without your direction.
You probably have a .html.erb template that renders the instance variables when an HTML request is received (format.html).
For the xml and json responses, you need to tell Rails what to do. For instance, you could supply a template .xml.builder.
Rails can also render certain structures (arrays etc.) for you automatically, simply by calling render json: #one
Rails goes through the registered formats and tries to find a compatible format, otherwise, it will raise an error.
Example:
def index
#stories = Story.all
end
index action does not have a respond_to block. If a client asks to get the page in TEXT format, it will lead to the following exception:
ActionView::MissingTemplate (Missing template blogs/index ... with { ... :formats=>[:text], ...})
We can easily fix this by adding a respond_to block:
def index
#stories = Story.all
respond_to do |format|
format.html
format.js
end
end
After the change, the client will get 406 error when the format is not supported. Also, your index action will respond to two new formats: js and HTML.
This article explains all the ways you can use respond_to blocks.

What is "Different Representations of Resources" in REST stand for?

Is that just means different file type?
I'm trying do this in ROR:
formatted_book_structures_url(book,"choose_list")
I suppose to deal with:
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #structures }
format.choose_list { render :partial => "choose_list" }
end
but the error gives me : uninitialized constant Mime::CHOOSE_LIST
Am I misunderstanding something?
The respond_to block expects you to use a MIME type such as html, json, xml, etc. You can add other custom types (see http://weblog.rubyonrails.org/2006/12/19/using-custom-mime-types) however Rails provides most of the more common types. To fix this you need to change choose_list to something else.

How do I make a json request to a rails 3 website?

I know that if I have a url like
mysite/posts/1
The default type returned to me is html. I can get an xml version of the resource by just doing
mysite/posts/1.xml
But how do I get a json version? Is the following supposed to work?
mysite/posts/1.json
Reason I ask is because it doesn't seem to be working. So I figured I should find out if it's "supposed" to work this way before investigating further.
You're doing it right, but if the Controller isn't setup to respond to json requests you won't get anything. You'll have a respond_to block like this:
respond_to do |format|
format.html
format.xml { render :xml => #model_var.to_xml }
format.json { render :json => #model_var.to_json } #without this line, .json requests will go unanswered by the web server.
end

Resources