开发者

how to use ajax in rails3.1?

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜