Matt The Bat
Matt The Bat

Reputation: 23

How to preprocess files in the "public" folder in Rails?

I've got a javascript file that I need to be accessed from outside my site (from an external site), but I also want to pre-process the file (ie. with Rail's Asset pipeline) so I can make use of some environment variables in the JS File.

Any idea how to do this? If I put the JS file into the public folder, I cannot pre-process it. However, if I put the file into the assets folder structure I can pre-process it, however, it is then not public.

Please excuse my ignorance, I'm new to this! ;)

Cheers,

Matt.

Upvotes: 0

Views: 206

Answers (2)

ichigolas
ichigolas

Reputation: 7725

What you put in your assets folder is indeed public. If it weren't you could not link to javascripts, stylesheets and images there. The only problem is that rails hashes the name of the files for cache busting purposes (only in production).

As a workaround you may create a controller action that gets the hashed file path using the javascript_url helper and redirects to it.

class ThisController < ApplicationController
  def some_action
    redirect_to ActionController::Base.helpers.javascript_url('javascripts/your_file')
  end
end

Upvotes: 2

Verty00
Verty00

Reputation: 743

Maybe try using grunt to build a task that exports the processed javascript file to the public folder and overwrites the old version everytime it builds.

http://developers.mobilesystem7.com/blog/post/replacing-rails-asset-pipeline-with-grunt-bower-browserify/

Upvotes: 0

Related Questions