Using Rails 3.1, where do you put your “page specific” JavaScript code?

前端 未结 29 1818
一生所求
一生所求 2020-11-22 11:08

To my understanding, all of your JavaScript gets merged into 1 file. Rails does this by default when it adds //= require_tree . to the bottom of your appl

29条回答
  •  无人及你
    2020-11-22 11:29

    Philip's answer is quite good. Here is the code to make it work:

    In application.html.erb:

    Assuming your controller is called Projects, that will generate:

    Then in projects.js.coffee:

    jQuery ->
      if $('body.projects').length > 0  
         $('h1').click ->
           alert 'you clicked on an h1 in Projects'
    

提交回复
热议问题