I am adding some view specific .js code to the header of the page using content_for like so

<% content_for :head do %>
  <script lang="text/javascript">
    $.getJSON("<%= book_chapters_path(@book) %>", function(data){

    });
  </script>
<% end %>

now how would I switch that js code to coffescript, can I put in a code block to tell rails to convert it to javascript, or even just add "lang/coffeescript" and rails would then just handle it.

有帮助吗?

解决方案

其他提示

In layout:

<% content_for :head do %>
  <%- javascript_tag do %>
    <%= render :partial => "cs/coffee-partial" %>
  <%- end %>
<%- end %>

Where cs/coffee-partial is a views/cs/_coffee-partial.html.coffee. You can even pass local variables to render and output them into your coffee-partial:

<%= render :partial => "cs/coffee-partial", :locals => {:version => '0.1', :name => 'varName'} %>

And then:

lib =
  version: '<%= version %>'
  '<%= name %>': 'some value'
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top