1

So I have an HTML file that I am transposing to an ERB view in my Rails app.

However, in the HTML, I have a snippet of JS that has to be run last (after everything else) because it handles the way the page scrolls.

So we have all of the HTML, then right before the </body>, I have this:

<script>
   // Some awesome JS that controls the scrollbar
</script>

But I haven't figured out how to make it work within Asset Pipeline, following Rails conventions, properly.

This file is my Home#Index, which is the marketing website for my app for non-logged in users.

If I put this <script> tag at the top of the layout that governs this Home#Index, it doesn't work.

If I move this JS to be inside my app/assets/javascripts/home.js that also doesn't work, because it loads home.js before it loads application.js.

I also don't want to put this inside application.js because it will interfere with other classes throughout the rest of my app that are similarly named.

What's the best RAILSy way to do this without just doing <script>JS</script> inside my app/views/home/index.html.erb, because that just feels wrong.

1 Answer 1

1

Below steps may solve your problem:

step:1 app/views/home/index.html.erb (provide source of JS.)

<%= javascript_include_tag 'custom/xyz.js', 'data-turbolinks-track' => true %>

step 2: assets/javascrips/custom/xyz.js (JS goes here)

$(function(){
  //js code goes here
});

step 3: application.js  (the require_directory directive which includes all JavaScript files only in the directory specified, without recursion.)

//= require_directory .

step 4: application.rb

config.assets.precompile += %w(custom/*.js)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.