0

I am building a rails app and in order to have page specific app.

I have added this to the bottom of my index.html.erb for my PagesController:

<script> </script>

The code sets the navbar opacity to 0 if it is at the top, but somehow this also shows up in the new.html.erb for my contactcontroller.

Any idea on how this is happening?

$(document).ready(function() {

    $(window).scroll(function() { /* run on load */

        if ($('.nav-fixed-top').css("opacity") == 0) {
            $('.nav-fixed-top').fadeTo(500, 1); /* fadeIn if not visible */
        } else {
            if ($(window).scrollTop() === 0) {
                $(".nav-fixed-top").fadeTo(500, 0);
            }

        };
    });
});

This is at the bottom of my index.html.erb for my PagesController. But it also seems to be getting called in new.html.erb for my ContactController.

GabLeRoux
  • 16,715
  • 16
  • 63
  • 81
  • Please show us your code. We can't help you if we can't see what you are doing. – Sumner Evans Oct 03 '17 at 01:14
  • I edited the question, there was an html `` tag hidden in there . Welcome to stackoverflow . Please use markdown syntax correctly – GabLeRoux Oct 03 '17 at 01:20
  • Thank you Gabe and Summer. I have added the code for you guys to see. – Eric Poretsky Oct 03 '17 at 01:21
  • I suggest you give a look at this [Stackoverflow question: How to do per page javascript with the rails asset pipeline](https://stackoverflow.com/questions/8976430/how-to-do-per-page-javascript-with-the-rails-asset-pipeline) – GabLeRoux Oct 03 '17 at 01:25
  • GabLeRoux you are my savior. TYSM – Eric Poretsky Oct 03 '17 at 01:29
  • I recorded a screencast on page specific javascript a while back that may be of some assistance as well. Might give you a different approach. https://www.driftingruby.com/episodes/page-specific-javascript-in-ruby-on-rails – kobaltz Oct 03 '17 at 03:59

1 Answers1

0

I can't speak as to why exactly that's happening within a script tag, but I do know that it is a best practice to use as few tags as you possible can in a Rails application. What I would do to resolve this issue is to put that in the pages.js file and use this -

var new_page_regex = new RegExp(/pages\/new/);

if(new_page_regex.test(window.location.pathname) {
  /* your code here*/
}
colincr
  • 550
  • 1
  • 7
  • 25