erb in coffeescript (Rails 3.2.13)?

I am using Rails 3.2.13.

After looking at some posts here, it looks like in order to use erb in CoffeeScript, your file just needs to have a .js.coffee extension, not .js.coffee.erb (weirdly).

However, I still cannot seem to get it to work. For example, I have the following in a demo.js.coffee file:

console.log '<%= root_url %>'

Which, after all processing, seems to produce:

(function() {
  console.log('<%= root_url %>');

  $(function() {});

}).call(this);

As you can see, the CoffeeScript is processed, but the erb isn't. What's going on?

Upvotes: 2

Views: 3305

Answers (1)

omarshammas
omarshammas

Reputation: 631

It is confusing, in some cases the .erb extension is required and in some it is not. I've found that when dealing with coffee script files under the assets directory it is required, however, when within the views it is implied.

So in your case it should be called demo.js.coffee.erb

The second problem, is that the erb is being evaluated outside the app context, so you require the following lines

'<% url = RailsAppName::Application.routes.url_helpers %>'
console.log '<%= url.root_path %>'

A similar question can be found here Route helpers in asset pipeline

Upvotes: 6

Related Questions