是否可以在不使用 respond_to 方法和使用非常规文件名的情况下渲染 js.erb?

发布于 2024-12-01 18:06:38 字数 990 浏览 2 评论 0原文

是否可以在不使用 respond_to 方法的情况下呈现 js.erb 以响应 AJAX 请求?

我这样问有几个原因:

  1. 我只会对我的控制器进行 AJAX 调用;
  2. 我不会支持任何其他内容类型(即:html、:xml 等);我
  3. 希望能够渲染一个不同的 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:

  1. I will only be making AJAX calls to my controllers;
  2. I will not be supporting any other content types (i.e., :html, :xml, etc.); and
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呆萌少年 2024-12-08 18:06:38

虽然 Kieber S. 的答案是正确的,但我认为这是另一种方法,对于那些想要支持创建 js.erb 文件的“传统”方法的人来说是有价值的。

不要使用 render :partial,而是使用 render :template。不过,这里需要注意的是,模板的搜索路径不会自动将范围限定为控制器的名称(即 app/views/posts)。相反,Rails 从 app/views/ 开始。因此,要引用“模板”,您需要添加要查看的子文件夹(如果它不在根目录中)。就我而言,我必须添加帖子

def create
  @post = Post.new(params[:task])
  if @post.save 
    # using :template and added 'posts/' to path
    render :template => 'posts/awesome.js.erb'
  end
end

这样做的好处是,如果您碰巧想要使用 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 use render :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 at app/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 add posts.

def create
  @post = Post.new(params[:task])
  if @post.save 
    # using :template and added 'posts/' to path
    render :template => 'posts/awesome.js.erb'
  end
end

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 your js.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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文