0

I'm using Rails 4 and have an app deployed on Heroku. I've been precompiling my assets locally, but my javascript isn't working as expected.

my application.js file looks like this('custom-scripts' has all my important functions in it):

//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//= require turbolinks
//= require jquery.slick
//= require custom-scripts
//= require_tree .

On Chrome's dev tools under the Resources tab I see that Frames/appname/Scripts/application.js exists with the code in 'custom-scripts.js' at the bottom of the file. In the Elements tab, the script tag is linking to /assets/application.js but under the Sources tab inside appname/assets/ I don't see application.js only application.css. I'm using the <%= javascript_include_tag 'application' %> tag so it should link correctly right?

I've been reading about the Assets pipeline and everything I'm seeing says that everything should be working. I wrote a test() function that just returns a console.log function in my custom-scripts file and when called in the Console tab, it returns 'function not defined' so that tells me my functions aren't even running for some reason. Any idea what could be wrong?

2 Answers 2

0

<%= javascript_include_tag 'application' %> yes, it is correct but it only pre-compile your scripts. have you tried creating a file in the assets/javascript? If you have user_controller.rb in the controller folder, i usually create a coffee script pertaining to that controller. i.g users.js.coffee there you can create or call your custom-script:

$(document).ready ->
         ...

I hope it helps mate. gl

Sign up to request clarification or add additional context in comments.

1 Comment

I do have other empty js.coffee files that were generated for my controllers, but I don't use them because the functions I wrote I need fore all my pages/views. So I just created a custom-scripts.js file and put them in there.
0

I think this is more of a dirty fix, but it works, so for now this is my solution; So I removed //= require_tree . and //= require custom-scripts from application.js and added <%= javascript_include_tag 'custom-scripts' %> to my application.html.erb file then ran rake assets:precompile again, pushed to github & heroku and bam, its working wonderfully.

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.