0

I am completely new to Ruby on Rails and I'm trying to serve an application in which an html file calls javascript that is located in a separate file. I placed the javascript file in the javascripts fold of my rails application. My html displays fine but when I press a button that is intended to call a javascript function I receive an error.

Uncaught TypeError: Cannot call method 'myMethod' of undefined

I can see that the file is present by looking in the resources tab of google chromes developer tools. It also looks like rails is correctly including the javascript file in the html file.

<script data-turbolinks-track="true" src="/assets/myScript.js?body=1"></script>

I'm not sure what I'm doing wrong. Any help would be greatly appreciated.

4
  • 1
    looks like it's the error of JavaScript itself nothing to do with rails... can you paste the code of it? Commented Mar 17, 2014 at 4:47
  • Show us the code, man! We want to help you! Commented Mar 17, 2014 at 4:53
  • True. "of undefined" suggests that "myMethod" is a function on an object that isn't defined. Post myScript.js and the line of HTML that's attempting to call the function "myMethod". Commented Mar 17, 2014 at 4:56
  • github.com/jacthoma/sequencewithme. The html file is the same index.html from that github link, minus the javascript and css includes. The javascript is sequence.js in the javascript folder. I'm trying to reimplement the same web app using ruby on rail, for a class. Everything works fine with node.js so I'm quite a bit confused. Commented Mar 17, 2014 at 5:25

1 Answer 1

1

You should really use the javascript_include_tag helper to keep the app to convention:

<%= javascript_include_tag "myScript" %>

The error is to do with your Javascript's call to myMethod. The error will be caused by referencing myMethod on an object / var which doesn't exist

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.