31

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.

Raymond Law
  • 1,188
  • 1
  • 10
  • 14

2 Answers2

51

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.

cjbottaro
  • 856
  • 9
  • 11
  • 2
    confirmed; the partial is ERB evaluated despite missing the extension, and render is able to find the file fine. – Teflon Ted Mar 28 '12 at 21:40
  • 1
    If you agree that this is confusing you might want to chime in on this issue: https://github.com/rails/coffee-rails/issues/36 – Silas Davis Jul 09 '13 at 09:38
  • 1
    A little update. As of Rails 4, you can (and must) add the .erb extension for it to be parsed as a template by erb before coffeescript. – IAmNaN Nov 21 '14 at 00:59
  • @IAmNaN up to Rails 4.1.9 though, view files named `action.js.coffee.erb` are not recognized as view templates – Olivier Lance Jan 20 '15 at 16:43
  • Actually even in Rails 4.2/coffee-rails 4.1.0 this extension is not recognized... – Olivier Lance Jan 20 '15 at 16:57
  • @OlivierLance: That's puzzling. My project has two .js.coffee.erb files and both are working. rails is at 4.2.0 and the coffee-rails gem is 4.1.0 too. No related initializers. – IAmNaN Jan 20 '15 at 17:26
  • Damn. I can't get it to work! Here's a test project: https://ide.c9.io/olance/erbcoffeetest A remote link to the "test" action rendered using a `.js.coffee.erb` view... Rails complains it cannot find the view. – Olivier Lance Jan 21 '15 at 10:05
4

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)

Olivier Lance
  • 1,738
  • 17
  • 30