How to use ajax in rails3.1?

As we know, Rails 3.1 uses CoffeeScript and jQuery.

The old way I process ajax request is to respond to the request with a template whose name is some_action.js.erb

Now that we have CoffeeScript, and I want my template to be able to use the coffeescript syntax. So what can I do?

Just change the suffix name of the template file?

some_file.js.coffee this works fine in the assets / javascript directory.

But I am processing the ajax request, and its template is under the views folder, just changing the template name from some_action.js.erb to some_action.js.coffee.erb or some_action.js.coffee , this will not work, the rails treat it like a regular file and will not compile it from coffeescript syntax to regular javascript.

I am very sorry for my Enlgish ability, hope this time my question will explain my purpose.

+4
source share
1 answer

Assuming you upgraded to Rails 3.1, using the new syntax as you said is as simple as converting your files to CoffeeScript.

Now you can’t just name the * .js.coffee file and expect a conversion, but Ryan Bates at Railscasts recommends changing the files manually as a great way to learn the syntax yourself. In fact, he has a screencast that he wrote that way: http://railscasts.com/episodes/267-coffeescript-basics

Rails will then process the compilation from js.coffee to .js for you.

+2
source

All Articles