Tuesday, November 28, 2017

Rails .js.erb templates no longer work with Webpack

Leave a Comment

I've just switched my Rails app over to use WebPack to deal with assets etc. It's working well apart from I have some JS templates in my views directory (*.js.erb) . These require jQuery and as jQuery is pulled in as part of my WebPack bundles, it is not working in those templates.

Is there a way to allow those templates to work?

1 Answers

Answers 1

Well in order to make things work you need to include jquery using yarn which works with the latest version of rails.

In Rails 5.1 this is done with the new JavaScript packet manager Yarn which you have to install first

sudo apt-get install yarn 

Then you can use it to install jQuery:

yarn add jquery 

To actually load jquery you have to add this line

//= require rails-ujs //= require jquery //= require turbolinks //= require_tree . 

After installing jquery your js.erb file will start working Refer this article

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment