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

前端 未结 29 1858
一生所求
一生所求 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:39

    Step1. remove require_tree . in your application.js and application.css.

    Step2. Edit your application.html.erb(by rails default) in layout folder. Add "params[:controller]" in the following tags.

    <%= stylesheet_link_tag    'application', params[:controller], media: 'all', 'data-turbolinks-track' => true %>
    
    <%= javascript_include_tag 'application', params[:controller], 'data-turbolinks-track' => true %>
    

    Step3. Add a file in config/initializers/assets.rb

    %w( controller_one controller_two controller_three ).each do |controller|
      Rails.application.config.assets.precompile += ["#{controller}.js", "#{controller}.js.coffee", "#{controller}.css", "#{controller}.scss"]
    end
    

    references: http://theflyingdeveloper.com/controller-specific-assets-with-rails-4/

提交回复
热议问题