samedi 27 juin 2015

How to reference a JS file from within application_helper

I have these files:

- app
-- assets
--- javascripts
---- controllers
----- company.js.coffee
----- projects.js.coffee

In my application.rb I got:

Rails.application.config.controllers_with_assets = %w( company projects )
Rails.application.config.controllers_with_assets.each do |controller|
  config.assets.precompile += ["controllers/#{controller}.js"]
end

In my application_helper I got this method set up:

def controller_assets
  controller = params[:controller]
  if Rails.application.config.controllers_with_assets.include? controller
    javascript_include_tag(asset_path('controllers/'+ controller))
  end
end

This is included in the layout so that js is only loaded if the appropriate controller is also active. This all works fine in development.

In production I can see that the JS files are correctly compiled.

/public/assets/controllers/company-28b5effa0fbec2899df9a18ab1b85975.js

In the view (browser) however I see that it failed to load:

GET http://ift.tt/1TVw0kO 

When I log in the console of the server and give this command:

Rails.application.assets.find_asset('controllers/company')
   => #<Sprockets::BundledAsset:0x5810768 pathname="/websites/instalane/production/releases/20150627231331/app/assets/javascripts/controllers/company.js.coffee", mtime=2015-06-27 14:21:45 +0000, digest="e7654520e52697294bb9e13ea09710e6"> 

What am I doing wrong?

FYI I already tried the following in the application_helper:

javascript_include_tag('controllers/'+ controller)

and

javascript_include_tag('controllers/'+ controller + '.js')

and

javascript_include_tag(controller)

Aucun commentaire:

Enregistrer un commentaire