5

I'm using Rails 3.2, It's set up for coffeescript. I know Coffeescript is an awesome language and it's not too hard to learn, but i'm JUST starting to wrap my head around Javascript and jQuery. So my question is this: Is there an easy way to set rails 3.2 up to use Javascript instead? At the moment, my jQuery is in <script></script> tags in my view (timeline/index.html.erb). I'd like to move it into a .js file. I tried changing the name of my timeline.js.coffee to just timeline.js and putting the jQuery in there, but I get Uncaught SyntaxError: Unexpected token ILLEGAL.

What should I do besides the obvious answer of "learn coffeescript"?

0

4 Answers 4

6

I was on the right track. To switch to javascript in rails 3.2 you only need to remove the .coffee extension. However, you also need to make sure you use // for comments instead of #

//# Place all the behaviors and hooks related to the matching controller here.
//# All this logic will automatically be available in application.js.
//# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
Sign up to request clarification or add additional context in comments.

Comments

1

Manifest Files and Directives
http://guides.rubyonrails.org/asset_pipeline.html#manifest-files-and-directives

add js files in app/assets/javascripts, then add the files in application.js


your js file

alert("here");

application.js

//= require_tree .
or
//= require your_js_file_name

then, it will be picked up from the code below in your app/views/layouts/application.html.erb

<%= javascript_include_tag "application" %>

1 Comment

it's all set up that way by default. except that the default filese are .js.coffee and i tried to change it to .js
0
$ rails -v
Rails 3.2.11

how about this?
http://bit.ly/VHEnBX

git clone it
cd js_test
bundle
rails s -d
open http://localhost:3000

# stop the detached server 
kill -9 `cat tmp/pids/server.pid`

you should see here alert from the browser.

it's just a simple js file. no .coffee extension or whatsoever. http://bit.ly/UPe2mp

Comments

0

you can simply comment out gem 'coffee-rails' in your Gemfile

1 Comment

It still should recognize .js as javascript with or without coffee-rails. Which it does.

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.