how to use ajax in rails3.1? - ruby-on-rails

As we know, Rails 3.1 is using CoffeeScript and JQuery.
The old way that I handle the ajax request is respond the request to a template which name is some_action.js.erb
Now that we have CoffeeScript and I want my template could use coffeescript syntax.So what can I do?
Just changing the template file's suffix name?
some_file.js.coffee this works fine in the assets/javascript directory.
But I am handle the ajax request and its template is under the folder views,if just simply change the template name from some_action.js.erb to some_action.js.coffee.erb or some_action.js.coffee,it won't work,rails treat it like normal file and will not compile it from coffeescript syntax to normal javascript.
I am very sorry about my Enlgish ability,hope this time my question will explain my purpose.

Assuming you have upgraded to Rails 3.1, taking advantage of the new syntax is, like you suggested, as easy as converting your files over to CoffeeScript.
Now, you can't just name the file *.js.coffee and expect a conversion, but Ryan Bates at Railscasts recommends changing your files over manually as a great way of learning the syntax yourself. In fact, he has a screencast posted of him doing just that: http://railscasts.com/episodes/267-coffeescript-basics
Rails, then, will handle the compilation from js.coffee into .js for you.

Related

Rails 3.2+ best practice for using Haml to generate Javascript templates (JST)

I asked a specific question about problems I'm having with a specific gem intended to do this in a separate thread ( https://stackoverflow.com/q/18577033/1206117?sem=2 )
But I feel I may be on the "wrong boat" somehow because all of the questions I find about Rails/Haml/JST-templates are at least 2 years old, or go unanswered.
I'm writing an app with a lot of client-side JS and so want to use templates to render views (I'm using Backbone). I want to use Haml to write the templates.
I'm not looking for a debate about which gem/method is better, I'm looking for A WAY that works and has current support and active use. At present I cannot write my JS templates in Haml, and it's a bummer. I'm avoiding CoffeeScript at present since I'm still rather new to Javascript.
I've submitted an issue to the haml git repo.
https://github.com/haml/haml/issues/716
Looks like https://github.com/netzpirat/haml_coffee_assets gives you what you want. (window.JST templates, written in HAML, with inline coffescript support)

Is it possible to share HAML templates between Coffeescript (Backbone.js) and Rails 3.2?

Is this possible? I know people use mustache to share templates, but can you use HAML? I would prefer pre compilation.
A quick google search turns up this lib for client-side rendering of HAML . So it would appear that yes, you could share templates between the client-side and server-side of your app.
If you are processing your JS templates server-side first, then the asset pipeline will take care of this with the correct file extension. You wont be able to use the rendered haml template on the client-side though as it will be missing its tags..

Coffeescript and Haml with unobtrusive Javascript (data-remote) in Rails 3.1

I searched le interwebs, but I haven't found someone experiencing the same problem as me, so I propose my question here.
I just started using Rails 3.1 with Compass, Haml and CoffeeScript and ran into a problem. When I rename my controller-specific JavaScript file located in app/assets/javascript/index.js to index.js.coffee and translate the JavaScript code to CoffeeScript, everything works as expected - the file is requested by the browser and compiled on the fly into JavaScript. Changes in the CoffeeScript file do also trigger recompilation.
However, when I try to do this with unobtrusive JavaScript (:remote => true) and rename the already working JavaScript file located in the view folder app/views/index/index.js.haml to index.js.coffee.haml and translate the included code, Rails doesn't recognize it as a CoffeeScript that needs to be compiled.
What am I doing wrong? Do I actively have to enable CoffeeScript evaluation for the view? Where?
The asset pipeline introduced in Rails 3.1 will automatically compile coffeescript assets into javascript for you, as you've mentioned. This is currently NOT the case for views: only files in app/assets/javascripts will be compiled.
To use Coffeescript in views, you'll need to use https://github.com/markbates/coffeeBeans for the time being.
Update : per Kyle Heironimus' comment below, it seems this functionality can be provided by coffe-rails (see https://github.com/rails/coffee-rails and http://rubygems.org/gems/coffee-rails )

Which extension should I use for view templates in Rails?

As far as I know, .rhtml is deprecated in Rails 2. In some guides there is only .erb, but both RubyMine and NetBeans IDE generate .html.erb and I've also seen it many people using.
I've tested both and they seem to work fine without any errors or warnings, but which one is correct? .erb or .html.erb
file_name.format.extension is the accepted convention now, where format is html, xml, json, anything really and extension is erb if you're using erb, haml if you're using haml and so on.
Typically you want to have the format in the file name.

Rails 3.0 beta & JS Helpers with jQuery

In the release notes for Rails 3.0 beta it says:
"Unobtrusive JavaScript helpers with drivers for Prototype, jQuery"
So how do I setup Rails 3 to use jQuery then? It still loads all the Prototype libraries by default.
I took this to mean that Rails 3 has built in functionality similar to the jRails plugin, but maybe I'm misunderstanding :)
Also, as a bonus question, if I am using Prototype is there a way to get Rails to load the minified versions, and even better a single concatenated JS file to cut down on http requests?
Thanks.
When you create a Rails 3 app, just pass along the -J param as well:
$ rails app_name -J
This will skip over including the Prototype libraries. Now, all you need to do is drop the latest jquery.js file into the public/javascripts directory. Once you do that, you'll also need the jQuery version of the rails.js file. You can get that here:
http://github.com/rails/jquery-ujs/blob/master/src/rails.js
EDIT: You need to include these files in the top of your layouts to gain the functionality. You can do this by:
<%= javascript_include_tag "jquery", "rails" %>
Hope this helps!
Unobtrusive JS doesn't refer to the PrototypeHelper methods, but to remote forms and links and the like. The concept is that you include :remote => true in your form_for or whatever helper methods support it, and then a driver called rails.js will look for those remotes and intercept the submit or click events and send them via xhr.
To use jquery you'll just need to replace the prototype ujs driver (which ships with rails) with the jquery ujs driver, that was extracted into its own repo shortly before the rails 3 beta release. You can find it here.
Check out Google Closure
It can turn multiple javascript files into a single compressed js file. It can even figure out which parts of the library you aren't using and remove them as well.
I don't know about Rails 3, but I'll try to answer bonus question.
You can put whatever you want in public/javascript directory. By default it will load files: prototype.js, effects.js, dragdrop.js and controls.js (read more). If you want to compress all js files and send it in one file, you can use this plugin.
Of course it won't work for dynamicaly generated js files.
I'm also working on that with trying to convert my old ajax with rails 3.
From what I can tell, they moved to a structure that adds a data-remote=true and when you add :remote => true to something like link_to which is supposed to replace link_to_remote in rails 3, so there are no more onclick methods that calls Ajax methods.
So, how does Ajax work in Rails 3, then? Well, you are supposed to have javascript methods that watch for when you click links that have a property of data-remote=true and does something with it, but don't actually include them in Rails (from what I can tell), it's library agnostic since you can write these methods to watch for clicks in prototype, jquery, write them yourself, or whatever else is out there.
I did find some javascript on github to get started that will watch for these events:
In prototype
In jQuery
I think in order to actually load jquery instead of prototype, you're going to have to just download it to public/javascripts and manually specify jquery, use javascript_include_tag :all, or override javascript_include_tag (not recommended)

Resources