是否可以在不使用 respond_to 方法和使用非常规文件名的情况下渲染 js.erb?
是否可以在不使用 respond_to
方法的情况下呈现 js.erb 以响应 AJAX 请求?
我这样问有几个原因:
- 我只会对我的控制器进行 AJAX 调用;
- 我不会支持任何其他内容类型(即:html、:xml 等);我
- 希望能够渲染一个不同的 js.erb 文件,而不是与控制器方法同名的文件(即 different.js.erb),
这是一些代码作为示例。
app/views/posts/awesome.js.erb:
alert("WOOHOO");
PostsController#create
def create
@post = Post.new(params[:task])
if @post.save
render :partial => 'awesome.js.erb'
end
end
当通过 AJAX 调用 create
方法时,Rails 会抱怨部分丢失的:
ActionView::MissingTemplate (Missing partial post/awesome.js, application/awesome.js with {:handlers=>[:erb, :builder, :coffee, :haml], :formats=>[:js, "application/ecmascript", "application/x-ecmascript", :html, :text, :js, :css, :ics, :csv, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json], :locale=>[:en, :en]}. Searched in:
Is it possible to render a js.erb in response to an AJAX request without using the respond_to
method?
I am asking because of a few reasons:
- I will only be making AJAX calls to my controllers;
- I will not be supporting any other content types (i.e., :html, :xml, etc.); and
- I want to be able to render a different
js.erb
file, not one named the same as the controller method (i.e.,different.js.erb
)
Here is some code to serve as an example.
app/views/posts/awesome.js.erb:
alert("WOOHOO");
PostsController#create
def create
@post = Post.new(params[:task])
if @post.save
render :partial => 'awesome.js.erb'
end
end
When the create
method is called via AJAX, Rails complains about a partial missing:
ActionView::MissingTemplate (Missing partial post/awesome.js, application/awesome.js with {:handlers=>[:erb, :builder, :coffee, :haml], :formats=>[:js, "application/ecmascript", "application/x-ecmascript", :html, :text, :js, :css, :ics, :csv, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json], :locale=>[:en, :en]}. Searched in:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然 Kieber S. 的答案是正确的,但我认为这是另一种方法,对于那些想要支持创建 js.erb 文件的“传统”方法的人来说是有价值的。
不要使用
render :partial
,而是使用render :template
。不过,这里需要注意的是,模板的搜索路径不会自动将范围限定为控制器的名称(即app/views/posts
)。相反,Rails 从app/views/
开始。因此,要引用“模板”,您需要添加要查看的子文件夹(如果它不在根目录中)。就我而言,我必须添加帖子
。这样做的好处是,如果您碰巧想要使用
respond_to
方法并遵循约定,则无需通过删除下划线来重命名js.erb
。While Kieber S. answer is correct, here is another method that I think would be valuable for those who want to support the "conventional" method of creating
js.erb
files.Instead of using
render :partial
userender :template
. The caveat here, however, is that the search path for the template isn't automatically scoped to the name of the controller (i.e.,app/views/posts
). Instead, Rails starts atapp/views/
. So to reference the "template", you need to add the subfolder you want to look into, if it isn't in the root. In my case, I had to addposts
.The benefit here is that if you should so happen to want to use the
respond_to
method and follow convention, you wouldn't need to rename yourjs.erb
by removing the underscore.部分文件需要命名为带下划线。
尝试将您的部分重命名为
app/views/posts/_awesome.js.erb
Partial files need to be named as with a underscore.
Try to rename your partial to
app/views/posts/_awesome.js.erb