javascript_include_tag Rails 4 generating “/javascripts/” instead of “/assets” in production

后端 未结 5 1692
野趣味
野趣味 2021-02-01 02:31

I have a Rails 4 application with

<%= javascript_include_tag \"modernizr\", \"data-turbolinks-track\" => true %>

in the head. In deve

相关标签:
5条回答
  • 2021-02-01 03:03

    By default, Rails only precompiles application.js, application.css and any images it finds in the assets path. Therefore, in production mordernizr will not get precompiled and thus the javascript helpers will not be able to find the file.

    In order to fix the issue, you can add modernizr to the precompile list by modifying the following config in production.rb

    config.assets.precompile += ['modernizr.js']
    

    For more information see the Rails Guides

    0 讨论(0)
  • 2021-02-01 03:20

    It may be because this file needs to be in /vendor/assets/javascript instead of /app/assets/javascript. The Vendor folder is for javascript libraries, and the App folder is for your code.

    A better solution than adding a tag to your layout would be adding a script reference to your application.js and let the sass compiler compress and attach it to your main javascript file.

    If you don't get a definitive answer, check out: http://guides.rubyonrails.org/asset_pipeline.html#asset-organization

    0 讨论(0)
  • 2021-02-01 03:22

    I have a new application using Rails 4 deployed on Heroku with :

    <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
    

    my javascript application.(fingerprint).js called from the src: assets/application.js

    i think your problem come from something into your production.rb who define assets from another location.

    So maybe you can add Moderniz.js to

    config.assets.precompile = ['.js', '.css', '*.css.erb']

    in config/production.rb

    Or simply require modernizr script into your application.js

    //= require mordernizr

    and remove the modernizr script call into your layout.

    <%= javascript_include_tag "modernizr", "data-turbolinks-track" => true %>
    

    Can you check from where your application.js is served into your production environment ?

    0 讨论(0)
  • 2021-02-01 03:26

    Be sure to precompile your assets in production by running this command:

    RAILS_ENV=production bundle exec rake assets:precompile
    

    The Rails Guide on the asset pipeline can give you more details: http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets

    0 讨论(0)
  • 2021-02-01 03:27

    One of the usage statements for AssetUrlHelper indicates it will produce /javascripts/ urls like what you are seeing:

    # asset_path "application", type: :javascript # => /javascripts/application.js

    (from asset_url_helper.rb line 117 - [1])

    This code looks like it can only be reached if the precompiled asset is missing so it would appear that your asset compilation is not working (my deployments usually fail when that happens, so maybe yours isn't even firing).

    The same asset_url_helper.rb calls the /javascripts/ part 'extname' and uses the following map to know how to generate the name:

     # Maps asset types to public directory.
      ASSET_PUBLIC_DIRECTORIES = {
        audio:      '/audios',
        font:       '/fonts',
        image:      '/images',
        javascript: '/javascripts',
        stylesheet: '/stylesheets',
        video:      '/videos'
      }
    

    A new Rails 4 app has this in the config/environments/production.rb

      # Do not fallback to assets pipeline if a precompiled asset is missed.
      config.assets.compile = false
    

    which seems to match the behavior you are seeing.

    0 讨论(0)
提交回复
热议问题