73

I'm sharing a configuration yml file client side, that I need to also load on the server side, I've placed it inside app/assets/javascripts/configuration.yml

I can use #{asset_path 'configuration.yml'} inside a view to get the path, but I can't inside a controller. I could access directly using "#{Rails.root}/app/assets/javascripts/configuration.yml" but when deploying the filename gets the digest string appended.

How can I get the same path from a controller?

Marcel M.
  • 2,376
  • 2
  • 18
  • 24

1 Answers1

122
ActionController::Base.helpers.asset_path("configuration.yml")

Might also be good to put configuration.yml in a different folder to separate javascript from non-javascript files.

Artem Kalinchuk
  • 6,502
  • 7
  • 43
  • 57
  • 1
    Thank you! I didn't need to use `/assets/`. I can just call ActionController::Base.helpers.asset_path("configuration.yml") I also thought about putting the yml file on another folder, tried adding it directly to the app/assets folder, but I couldn't access it. Thanks for the suggestion anyway. – Marcel M. Oct 19 '11 at 20:42
  • 1
    @MarcelM. you need to add other folder to the search path in application.rb. Like so `config.assets.paths << Rails.root.join("app", "assets", "yml")` – Paulo Casaretto Jan 25 '13 at 12:04
  • This is deprecated in Rails 4. – Agis Jun 29 '15 at 08:11
  • 12
    @Agis — perhaps include the new recommended solution. – Michael Jul 27 '16 at 18:19