Rails performing request XHR when loading pages

3

I've noticed that in every page / route exchange, Rails * makes a new request Ajax to fetch this new data (HTML), and thus page exchange. I did a lot of research but the closest I got was to Embedded Ruby ( erb ).

If this is really valid, I would like to know how and with what Rails does this exchange, and what are the options and advantages of using this type of resource, for example:

  • Do I have the availability of the two-way data binding technique as well?
  • What resources are available?
  • Do I have other options?

Details

The question can be confusing so I decided to add some pictures that show what I really want to understand. Using a simple navigation bar with links (element a) for the exchange of routes, as shown in figure 1.

Figure1:ThenavigationmenuIusedfortesting.

Icanseethatrailsmakessomerequeststochangethesepages(suchasangularjsorvuejs),asshowninfigure2.

Figure2:Theresultsofpageexchanges/routesontheconsoletab

Wecanalsofollowthenetworktab,asshowninfigure3.

Figure 3: The results of page exchanges / routes on the network tab

Thank you.

    
asked by anonymous 11.07.2016 / 19:51

2 answers

1

I think these requests that you are logging into your service are being made by Turbolinks (Added in version 4 of the framework), unfortunately it does not have Two Way databind , because its function is different. You can check the available features and other options in the Turbolinks documentation .

A general summary of how it works:

Turbolinks transform Rails applications into a single page JavaScript application; that is, instead of loading new pages, it replaces the current page with new content from the server:

  

The functionality is similar to pjax , but instead of worrying   with what element to replace on the page and then customize the answer   server according to the response, we replace the entire body of the   page. This gives the majority of the speed obtained   using pjax (without recompiling JavaScript or CSS), avoiding   customize server responses.

    
14.07.2016 / 14:40
0

Ruby on Rails is a framework for the backend and therefore is not a single-page application framework (for the frontend) such as AngularJS and React. When you request a request, it sends a response, which is a new page with the template related to the route you requested. (Note: If it is an API, the server sends a response in JSON instead of HTML.)

The routes are set to config/routes.rb and the templates are chosen based on the actions in the controller ( app/controllers/ ) that is associated with the route. Side note : Many things are done for you behind the scenes with Rails, so if you want to understand the route and template business further, I suggest you use the Sinatra , which is a web framework similar to Rails, although much lighter and requires you to make your own routes and send the templates manually.

I think you're confusing the frontend with the backend. Normally the two are separate and Rails only serves as a backend that sends information via JSON. There in the frontend, using a framework like the Angular, you make requests to the backend to get the information via JSON, which you put in the DOM. It's in the frontend that you can do two-way data binding (with the Angular) or one-way (with React).

    
11.07.2016 / 21:25