Rails 3:RJS 与 JavaScript
我最近开始学习基于 RoR3 beta/RC 的 Ruby on Rails。我之前一直使用其他框架(例如 Django)开发应用程序,其中 JavaScript 完全是我自己编写的。
使用 RoR 开发应用程序时,我对实现 JavaScript 的两种可能方法感到困惑:“纯”方法(编写 JavaScript 并将其放入 标记中)和编写 RJS 文件。
应该使用哪种方法以及何时使用?如果是 RJS,我如何实现 onChange
等特定操作?
I've recently started learning Ruby on Rails, based on RoR3 beta/RC. I had earlier been developing applications using other frameworks (like Django), where the JavaScript had been written completely on my own.
When developing application using RoR, I get confused by the two possible ways of implementing JavaScript: the "pure" one (writing JavaScript and putting it in the <script>
tag) and writing RJS files.
Which of the approach should rather be used and when? If RJS, how do I implement particular actions like onChange
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Rails 中,您应该使用这两种方法来完成不同的任务。我也是。当我想执行 ajax 请求时,我使用 RJS,因为它简化了请求,并允许使用更少的代码在 .rjs 文件中编写更强大、更灵活的响应。例如,可以在其中使用部分并访问 ruby 变量。当我想让页面更加动态时,我使用纯 javascript,将其与
javascript_include_tag
一起使用。UPD。
这种方法在 Rails 3 中也同样有效。可以将它与 JQuery 和 HAML 一起使用。 Rails 3 中的 UJS 甚至简化了它。如果您更喜欢 Jquery 而不是 Prototype,那么您唯一应该做的就是使用 jquery-ujs文件命名如
create.js.haml
。例如,可以使用 服务器端验证 在 AJAX 请求中使用这种方法。
In rails you should use both approaches for different tasks. So do I. When I would like to execute ajax request, I use RJS because it simplifies the request and allows to write more powerful and flexible respond within .rjs file with a much less code. For example it is possible to use partials in it and to get access to ruby variables. When I would like to make a page more dynamic, I use plain javascript including it with
javascript_include_tag
.UPD.
This approach works in rails 3 the same way. It's possible to use it with JQuery and HAML. UJS in Rails 3 even simplifies it. The only thing you should do is to use jquery-ujs if you prefer Jquery over Prototype and proper file naming like
create.js.haml
.For example it possible to use server-side validation in AJAX requests with this approach.
在 Rails 3 中,正确的方法是纯 JS。
RJS 太严格了,你必须使用 Prototype 或以某种方式 hack 才能将它与 jQuery 一起使用。
Rails 3 还提倡不引人注目的 JavaScript。在 AJAX 请求中,您应该返回嵌入 ERB 的纯 Javascript,而不是使用 RJS。
不幸的是,由于此更改需要对使用 RJS 的旧项目进行大量返工,因此有些人会保留它。
RJS 实际上在 Rails 3 中已经被贬值,您甚至需要一个插件才能使用它,所以如果您现在开始并且您足够幸运已经开始使用 Rails 3,请采用 Rails 3 方式。
这是新 UJS 方式的一个很好的例子:
http://railscasts.com/episodes/205-unobtrusive-javascript
祝你好运
In rails 3 the right way to do it is pure JS.
RJS is too strict and you have to use Prototype or hack in some way to use it with jQuery.
Rails 3 also promotes unobtrusive javascript. On AJAX requests you should return pure Javascript embed with ERB instead of using RJS.
Unfortunately since this change requires a lot of rework for older projects that used RJS some people will leave it be.
The RJS is actually depreciated in rails 3, you need a plugin to even work with it, so if you're starting now and you're lucky enough to start on rails 3 already, go the rails 3 way.
Here is a good example of the new UJS way:
http://railscasts.com/episodes/205-unobtrusive-javascript
Good luck