Can I use CoffeeScript in the views executed on render.js?

后端 未结 4 1451
长发绾君心
长发绾君心 2021-02-07 20:18

What do I need to do so that I can use CoffeeScript in the Rails JS views? For example:

def index
    format.js { render :layout => false } 
end
4条回答
  •  长情又很酷
    2021-02-07 21:16

    This is now working in Rails 3.2. For example, I have a resource named book. This resource has a file at app/views/books/index.html.erb with the following:

    <%= link_to 'test me', new_book_path(color: 'blue'), remote: true %>
    

    Then I have a file at app/views/books/new.js.coffee at ≈ with the following code:

    test = ->
      'this is a test'
    
    console.log test()
    console.log( "<%= params[:color] %>" )
    

    I see:

    'this is a test'
    'blue'
    

    in my browser console.

提交回复
热议问题