I haven't been able to find documentation that talk about how create.js.erb
and destroy.js.erb
fit in a Rails 3 app.
我还没有找到关于create.js的文档。erb destroy.js。erb适合于Rails 3应用程序。
What is the logic behind these types of Javascript files in the app > controllers? How and when are they accessed?
>控制器中这些类型的Javascript文件背后的逻辑是什么?如何以及何时访问它们?
3 个解决方案
#1
2
File extensions are associated with Mime types. Check my answer here. It was written for Rails 2.2.2, but the same logic still holds. Note that the respond_to
syntax has changed in Rails 3.
Also, these files are not in app/controllers
, but in app/views/<controller_name>/
文件扩展名与Mime类型相关联。检查我的答案。它是为Rails 2.2.2编写的,但同样的逻辑仍然适用。注意,respond_to语法在Rails 3中发生了变化。而且,这些文件不在app/controller中,而是在app/views/
#2
2
When you access your action via XMLHttpRequest for example, your action will respond with a javascript file.
例如,当您通过XMLHttpRequest访问操作时,您的操作将使用一个javascript文件进行响应。
In your controller:
在你的控制器:
class MyController < ApplicationController
respond_to :html, :js
def show
@my_model = MyModel.find(params[:id])
respond_with @my_model
end
end
The show action will respond with the html view when accessed via html, and with je js view when accessed via XMLHttpRequest.
show动作在通过html访问时将响应html视图,在通过XMLHttpRequest访问时将响应jjs视图。
#3
1
Basicly those are the views rendered in respond to ajax call. When you do a normal request, then, then controller is passing the varaialbes to your view, ie. create.html.erb. If you do an Ajax call to the controller, then the controller is rendereing create.js.erb.
基本上,这些是响应ajax调用的视图。当你做一个正常的请求时,控制器就会把varaialbes传递给你的视图。create.html.erb。如果对控制器执行Ajax调用,则控制器将渲染create.js.erb。
The main difference is, that in the create.html.erb you should have a full template of your page. In case of create.js.erb you should have a javascript code which can modify your views.
主要区别在于,在create.html中。你应该有一个完整的页面模板。create.js。erb您应该有一个可以修改视图的javascript代码。
For example:
例如:
$('#comments-box').html("<%= escape_javascript(index_comments(@commentable, @comments)) %>");
$('#comments-box-spinner').hide();
$('#flash').html("<%= escape_javascript(render(:partial => 'layouts/flash', :collection => flash)) %>");
#1
2
File extensions are associated with Mime types. Check my answer here. It was written for Rails 2.2.2, but the same logic still holds. Note that the respond_to
syntax has changed in Rails 3.
Also, these files are not in app/controllers
, but in app/views/<controller_name>/
文件扩展名与Mime类型相关联。检查我的答案。它是为Rails 2.2.2编写的,但同样的逻辑仍然适用。注意,respond_to语法在Rails 3中发生了变化。而且,这些文件不在app/controller中,而是在app/views/
#2
2
When you access your action via XMLHttpRequest for example, your action will respond with a javascript file.
例如,当您通过XMLHttpRequest访问操作时,您的操作将使用一个javascript文件进行响应。
In your controller:
在你的控制器:
class MyController < ApplicationController
respond_to :html, :js
def show
@my_model = MyModel.find(params[:id])
respond_with @my_model
end
end
The show action will respond with the html view when accessed via html, and with je js view when accessed via XMLHttpRequest.
show动作在通过html访问时将响应html视图,在通过XMLHttpRequest访问时将响应jjs视图。
#3
1
Basicly those are the views rendered in respond to ajax call. When you do a normal request, then, then controller is passing the varaialbes to your view, ie. create.html.erb. If you do an Ajax call to the controller, then the controller is rendereing create.js.erb.
基本上,这些是响应ajax调用的视图。当你做一个正常的请求时,控制器就会把varaialbes传递给你的视图。create.html.erb。如果对控制器执行Ajax调用,则控制器将渲染create.js.erb。
The main difference is, that in the create.html.erb you should have a full template of your page. In case of create.js.erb you should have a javascript code which can modify your views.
主要区别在于,在create.html中。你应该有一个完整的页面模板。create.js。erb您应该有一个可以修改视图的javascript代码。
For example:
例如:
$('#comments-box').html("<%= escape_javascript(index_comments(@commentable, @comments)) %>");
$('#comments-box-spinner').hide();
$('#flash').html("<%= escape_javascript(render(:partial => 'layouts/flash', :collection => flash)) %>");