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

前端 未结 29 2292
一生所求
一生所求 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条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 11:26

    This is how i solved the styling issue: (excuse the Haml)

    %div{:id => "#{params[:controller].parameterize} #{params[:view]}"}
        = yield
    

    This way i start all the page specific .css.sass files with:

    #post
      /* Controller specific code here */
      &#index
        /* View specific code here */
      &#new
      &#edit
      &#show
    

    This way you can easily avoid any clashes. When it comes to .js.coffee files you could just initialize elements like;

    $('#post > #edit') ->
      $('form > h1').css('float', 'right')
    

    Hope this helped some.

提交回复
热议问题