13

I've looked around for a couple days now for a way to implement js.erb files into my Ruby On Rails 7 application and could not find any resources on this topic. Are js.erb files completely depreciated in Rails 7? If so, is there any way to call in partials in the javascript files of import map?

I'm trying to implement a javascript function that re renders and updates the "like" count on a post. My approach requires me to pull the count data from a table and change the element's text with that ruby partial.

Any resources or a point in the right direction would be greatly appreciated!

Aniket Tiwari
  • 3,561
  • 4
  • 21
  • 61
Kyowan Kim
  • 377
  • 3
  • 12
  • 1
    No it's not deprecated. If you prefer the js.erb files, then use them ! They can't deprecate it for backward compability reason, so if you prefer them it's very ok. – bdavidxyz Jan 16 '22 at 10:05
  • surely there's a plugin to do that in es-build or your equivalent bundler? – BenKoshy Jan 20 '22 at 21:44
  • In my case, I needed to change `render(template: "shared/something.js.erb")` to `render(template: "shared/something", format: [:js])` -- it took a while to figure this out so I'm hoping to save someone else some time. – Cymen Aug 03 '23 at 18:53

1 Answers1

-2

Not really.

With RoR 7, Javascript is handled using these ways:

  • Webpack
  • Import Map
  • ESBuild
  • Rollup

Regardless the handling technology used, the Javascript code is packaged during the development/deployment process. It can not be updated at runtime.

However, the need you expressed is easy to address. Use Hotwire, the new technology introduced few months ago and now part of Rails 7. Simply speaking, Hotwire stands for HTML over the wire. What it does is to send a partial over the wire in real time. Check on YouTube and you'll find plenty of tutorials explaining how to use Hotwire with Stimulus, React or Vue.

  • 1
    This is not what the OP asked. Also seems like a copy paste from somewhere. btw, I am still searching for an answer. – Jey Geethan Aug 31 '22 at 02:23