简体   繁体   中英

Rails - Formats and Rendering - How do they work?

From what I understand, calling render will return a response to the browser. It can return different types of content (html, json, js,etc). I am however, a bit confused on what the browser does afterwards.

If the response is html, the browser just renders the page. If the response is json, usually, the client js code takes the json response and does something useful with it. If the response is js, what would the browser do? I am not sure if this js is appended to the document and executed? Is this related to Rails UJS?

Also, how is render related to respond_to? Are they used side by side, or is one preferred over the other?

render html:
render js:
render json:

respond_to |format| do
  format.html
  format.js
  format.json
end

Sorry if this may seem like a lot of questions, but I think it would help if these questions were discussed together.

If the response is js, what would the browser do?

That's completely down to the browser and the users javascript environment. Your application returns js, the browser will handle that in whatever way it thinks best.

I am not sure if this js is appended to the document and executed? Is this related to Rails UJS?

UJS is more to do with ensuring that an application will work gracefully even if the users browser does not handle javascript. For example, you write your script to intercept a button click event and cancel the click action, instead doing something else, an AJAX call for instance. If the user has javascript disabled, this intercept won't occur. Because you've written the js this way, the normal button click action will occur. Both cases should be handled and both should work - that is unobtrusive use of javascript.

As for appending to the document, not exactly no. The browser receives a bunch of code and, if it is able, it runs that code in the context of the current document. It doesn't then save the code anywhere, when it completes it is discarded and the document is left in the whatever state the script left it.

how is render related to respond_to? Are they used side by side, or is one preferred over the other?

respond_to allows you to control how you respond to the user based on how they made their request. In the example of UJS above this allows you to have one method that responds to HTML and AJAX requests, but knows the difference and can make whatever changes it needs to make to its response.

Example:

respond_to |format| do
  format.html # will by default render the view with this actions name
  format.json do # lets pass back the object in json format
    render @object.to_json
  end
end

These are all intended as introductory answers and are quite simple, I would recommend further reading on each subject.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM