Reputation: 6653
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.
Upvotes: 0
Views: 871
Reputation: 599
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'
Upvotes: 3
Reputation: 1528
You can use this gem: https://github.com/markbates/coffeebeans
Upvotes: 1