(Rails) What is "RJS"?
Asked Answered
G

3

36

I've seen "RJS" and "RJS templates" mentioned in passing in blog posts and tutorials. I did a search, but I'm still unsure about it. Is it a technology specific to Rails, rather than a standard like JSON or YAML?

I understand it's used for "generating JavaScript." Does it generate generic JS or Rails-specific JS requiring the Prototype and Scriptaculous libraries?

Gunilla answered 19/1, 2009 at 23:43 Comment(0)
C
22

This Railscast gives a nice example of using RJS to add and remove form fields dynamically without hitting the server with an ajax call.

These RJS tips may also be helpful.

Chlorosis answered 20/1, 2009 at 0:26 Comment(0)
J
18

RJS was a "ruby-to-js" template system, RJS is not widely used anymore in the Rails ecosystem. SJR (Server-generated JavaScript Responses) is now prefered.

This DHH's post helped me a lot to understand : https://signalvnoise.com/posts/3697-server-generated-javascript-responses

2021 EDIT : now you should use Hotwire.dev

Josephson answered 13/5, 2015 at 0:14 Comment(3)
Just to elaborate on the "is not widely used anymore": it's 2020 and RJS is still used by Basecamp (company behind Rails - even after they had major refactor in 2018) and Shopify. From what the Author of Rails is saying it's not going to disappear any day soon. So RJS it's still ok to use it in production apps (I'm stlil using it in personal projects)Pengelly
@Pengelly ok ! I know Basecamp are working on a "new magic on the frontend", it should be ready for end of 2020 (cf dhh on twitter), I can't wait for them to show usJosephson
That ‘new magic’ is released at Hotwire, hotwire.dev, “ Hotwire is an alternative approach to building modern web applications without using much JavaScript by sending HTML instead of JSON over the wire.”Leopoldine
M
17

RJS is a template (similar to an html.erb file) that generates JavaScript which is executed in an eval block by the browser in response to an AJAX request. It is sometimes used (incorrectly?) to describe the JavaScript, Prototype, and Scriptaculous Helpers provided by Rails.

Misha answered 20/12, 2009 at 6:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.