Ajax and Rails
This is just a short post to save a few links and document that "Server Javascript Responses" in Rails are still useful in 2018/19.
Lifecycle of SJR
The basic idea behind Server Javascript Responses (SJR) is diagrammed below:
SJR works like this:
A User submits a form that has the Rails attribute
remote=true
on the form tag which makes the form submission happen via AJAX. See the Rails guide for more information.The server receives the data payload and processes a response.
The Response from the server is a blob of JS not JSON that usually comes from a matching
name.js.erb
template.The User's browser receives the JS blog and evals the JS to update the page.
Links
- DHH's original post
- Medium post about eschewing fancy front-end frameworks for SJR
- Ajax calls the Rails way