This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I've got this in a js.coffee file in my tutorial web app:
This makes a particular post, called a status, change color and display links to edit or delete the post. It only works when the page is first loaded into the browser as the index page, or when redirected to as root_url by the sign_out action. When the same page is loaded by clicking on a link using statuses_path, nothing happens when you mouse over the statuses. It's the same page, with the same url, but the script doesn't work after clicking one of the links. If I reload the page, or sign in and then sign out, the script works.
I didn't notice this problem until I decided to scratch devise and do the authentication from scratch, so I could add custom fields to the form that actually worked. I got the authentication working okay, but I can't figure out what is going on with this script. It seems to me that the same page should work the same, regardless of how you got there, but obviously I'm missing something here.
What can I do to correct this?
Joined: Mar 15, 2001
Okay, so I tried comparing the pages that were generated, and the html was identical. However, when I inspected the status element on each page, .status.hover
was being applied on the working page, but not on the broken page.
Could this be a CSS incompatibility somewhere? I have a custom.css.scss file that imports a bootstrap gem, the application.css and statuses.css.scss. Where should I look to figure this annoying little mystery out?
What would cause the coffeescript to stop responding to a hover when the page is accessed via statuses_path, but work okay via root_url?
Joined: Mar 15, 2001
Eureka! This is a known issue with turbolinks! Thank you Stack Overflow! The problem occurs because the page is not reloaded when the links are clicked, so the script is not called. To solve this, I installed the jquery-turbolinks gem and required it right after jquery in the application.js file. Now the hover effect works after clicking on a link, too.
I don't know why posting my questions here help me to solve my problems, but it does, even when I'm the only person responding to my posts. OMG......I'm talking to myself more and more....and now I'm responding to myself as well.
Oh well, it seems to be working for me!