简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM