如何在app/views中渲染new.js.coffee.erb?

发布于 2024-12-07 15:01:59 字数 335 浏览 2 评论 0原文

使用 Rails 3.1 jquery_ujs,我有一个链接:remote => true,控制器新操作以 js 响应并呈现 new.js.erb,其中包含:

$('#post-form').html('<%= escape_javascript(render(:partial => "form")) %>');

它呈现 _form.html.erb 部分。这有效。

现在我想使用 Coffeescript,但将 new.js.erb 重命名为 new.js.coffee.erb 不起作用。资产管道仅适用于应用程序/资产吗?我做错了什么?谢谢。

Using Rails 3.1 jquery_ujs, I have a link with :remote => true, and the controller new action responds with js and render new.js.erb which contains:

$('#post-form').html('<%= escape_javascript(render(:partial => "form")) %>');

It renders the _form.html.erb partial. This works.

Now I want to use Coffeescript, but renaming new.js.erb to new.js.coffee.erb doesn't work. Does the asset pipeline only work in app/assets? What am I doing wrong? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

不气馁 2024-12-14 15:01:59

我在使用 Rails 3.1.0 时遇到了同样的问题。尝试将文件重命名为 new.js.coffee。尽管文件名没有扩展名,它仍然应该呈现 erb。

视图模板不遵循与资产管道相同的约定,这绝对令人困惑。

I had this same issue using Rails 3.1.0. Try renaming your file to just new.js.coffee. It should still render erb despite not having the extension on the filename.

It's definitely confusing that view templates don't follow the same conventions as the asset pipeline.

迷乱花海 2024-12-14 15:01:59

如果您希望保留 .js.coffee.erb 扩展名,这里有一段 Rails 4 的代码,可以让 Rails 将该文件识别为有效的视图模板:(

# config/initializers/coffee_erb_handler.rb
ActionView::Template.register_template_handler 'coffee.erb', Coffee::Rails::TemplateHandler # without this there will be template not found error

class ActionView::PathResolver < ActionView::Resolver
  EXTRACT_METHODS = %w{extract_handler_and_format_and_variant extract_handler_and_format} # name for rails 4.1 resp. 4.0

  method_name = EXTRACT_METHODS.detect{|m| method_defined?(m) || private_method_defined?(m)}
  raise 'unknown extract method name' if method_name.nil?

  old_method_name = "old_#{method_name}"

  alias_method old_method_name, method_name
  define_method(method_name) do |path, default_formats|
    self.send(old_method_name, path.gsub(/\.js\.coffee\.erb$/, '.js.coffee'), default_formats)
  end
end

这是一个贡献 由 cervinka 发表于 Coffee-rails 问题 #36

If you wish to keep the .js.coffee.erb extension here's a piece of code for Rails 4 to have Rails recognize the file as a valid view template:

# config/initializers/coffee_erb_handler.rb
ActionView::Template.register_template_handler 'coffee.erb', Coffee::Rails::TemplateHandler # without this there will be template not found error

class ActionView::PathResolver < ActionView::Resolver
  EXTRACT_METHODS = %w{extract_handler_and_format_and_variant extract_handler_and_format} # name for rails 4.1 resp. 4.0

  method_name = EXTRACT_METHODS.detect{|m| method_defined?(m) || private_method_defined?(m)}
  raise 'unknown extract method name' if method_name.nil?

  old_method_name = "old_#{method_name}"

  alias_method old_method_name, method_name
  define_method(method_name) do |path, default_formats|
    self.send(old_method_name, path.gsub(/\.js\.coffee\.erb$/, '.js.coffee'), default_formats)
  end
end

(This is a contribution by cervinka on coffee-rails issue #36)

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