How to render new.js.coffee.erb in app/views?

穿精又带淫゛_ 提交于 2019-12-02 20:13:05

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.

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)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!