How to preprocess files in the "public" folder in Rails? - ruby-on-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.

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

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/

Related

how to have assets other than js and css files in rails

I have some object files which I want to have them in my JS codes I put them under app/assets/3d-models and when I try to get a link to it I use:
<%= asset_path("3d-models/splits2/xxx.obj") %>
But the output is /3d-models/splits2/xxx.obj which obviously the 404 NOT FOUND is the result(i.e the asset not found, the wrong link!)
Question:
How can I get an access link to a file other than common files used in rails' assets?
You can have different assets. Just place them into assets folder and link to them correctly.
Rails are using assets pipeline. This is a concept, which provides a better way to serve some static files to a website. In Rails it is implemented by Sprockets gem. Please read more here.
You can also have other static files directly in public folder and link to those files anywhere from your app. This will not be included in assets pipeline.
If you use assets_path helper method, then there is a fingerprint added to a file so the URL to the file is different then.
Please check this section.

Adding folders to rails public folder [duplicate]

Hello I'm new to rails.
In my RoR app folder I have a folder (app/FOLDER) that I want to make public (there are script files that need to avaliable from browser), how can I do this?
In the latest Rails the files should exist in one of the following...
app/assets
lib/assets
vendor/assets
If you cannot move the folder but still need it to be accessible you can use...
Rails.application.config.assets.paths << folder_path
Slightly more detailed answer over here...
Rails 3.1: The public directory no longer serves js assets. How to load an additional js file after page is loaded?
you shouldn't put them in app/FOLDERbut in public/FOLDER, everything going in public/ is public.
Here's an explaination of all the directories in a rails app : guides.rubyonrails.org

Prohibit Direct URL for public, Rails

I'm making a Rails application.
When user post content, it makes a folder in public folder. Only admin can put images in it. And user can watch it in folder he made.
But if other users put direct url like this http://railsapp.com/folder/test/test.jpg/, it shows.
I want to prohibit to show when somebody access directly. How should I do?
Do I make the folder in Rails.root? But I don't know how to show it. Please give me a advise.
The solution here is pretty obvious - don't upload to /public if you don't want it to be public. And no you don't want to change the access rules for your public directory as this will mess up all your assets and the rails fallback error pages.
The common choice is to use /storage but then you need to use Rails to serve the assets.
That would involve creating a model and a controller for the uploaded files and serving them with send_file.
class AttachmentController
def show
#attachment = Attachment.find(params[:id])
# #todo authorize access.
send_file #attachment.path, type: #attachment.mime, disposition: 'inline'
end
end
A workaround for cases when you don't want to tie up rails threads just with serving images is to use a non guessable hash in the path name to your "private" assets. Note that they are not really private - just harder to find.

Put files (css, html, js, images) per feature in one place in Rails 3?

Whenever I add a new feature (eg. something I downloaded) I tend to want to put all the files (css, html, js, images) in one place.
Symfony 2.0 will have this new feature they called bundle system. Everything will be in its own folder. This will be great for adding new features so you don't have to mix all css, js, image files with each other. It should be per feature instead.
And also it would be great for deleting features. Then you know that all files are in one place and don't have to look for them throughout your application.
Eg.
Instead of this...
images/
fader.img
cart1.img
cart2.img
javascripts/
fader.js
cart.js
stylesheets/
fader.css
cart_main.css
cart_sub.css
...you should have it like this...
venture/
fader/
fader.img
fader.css
fader.js
cart/
cart1.img
cart2.img
cart.js
cart_main.css
cart_sub.css
Is there a way of doing so in Rails 3?
Sure, you could just treat them like a plugin - making a set of files into a plugin is very simple, after all - you basically just put them in a folder, in a file structure parallel to the root of your rails app, then put that folder in your vendor/plugins folder.
Here's the guide on it: http://guides.rubyonrails.org/plugins.html
Then, if you want to delete a feature, just destroy it's plugin folder, and you're clean.

Directory to store cached files in Rails?

I am generating some large files in my Rails application. Fortunately, they only need to be generated once. I would like to save these files to disk so that I don't have to generate them again.
Which directory in my Rails application is the most appropriate place to put application generated files?
Thanks!
If security of the files is not an issue you can put them in a subdirectory of public (for example, public/assets) which in your deploy script is symlinked to a directory in shared/public so that when you redeploy the files are retained.
If security is an issue, the solution is similar, though you would not want the directory to be web accessible. Instead you would use a controller to control access and serve up the files with send_file.

Resources