Assets vs Public for static videos - ruby-on-rails

I have a static video in my rails application. I have two copies of it: one stored in assets/videos and one stored in public. For some reason the video stored in the assets folder loads much slower than the video stored in the public folder.
Does anyone know why?
What is the best practice? Is it bad practice to put it in public?

Does anyone know why?
Because public folder is the first place which serves incoming request. If the folder/file doesn't found in that folder, it will be forwarded to rails.
What is the best practice? Is it bad practice to put it in public?
it depends. If you want to use rails features like accessing videos folder with assets_path or may be like converting videos from one format to two with some rails script or you want restrict videos to some countries or people/user then you should put NOT put in public folder.
That's all.

The assets directory is only needed if you want to take advantage of
the asset pipeline. The asset pipeline handles things from
compressing and compiling .coffee and .less or sass files to
compressing your js and css into one file so your webserver only has
to serve one file for each.
When you compile your assets with the "rake task bundle exec rake assets:precompile" they are moved to your public directory anyhow
Check this comment https://stackoverflow.com/a/8581316/1231365
So it's better to keep your videos in the public folder.

Related

rails which is best place to save static html files

I have to save some html files and wanted to load them for some requirement.
Currently i am placing that files in public folder.
Also, i don't want to save it outside (amazon s3 etc) the rails application for some good reason.
Please let me know which is idea way to do such functionality in Rails.
I am using Rails 3.2.21
Actually I think /public is not a bad place for static html files. As long as you don't need any processing this should work well.
See here:
public − Like the public directory for a web server, this directory
has web files that don't change, such as JavaScript files
(public/javascripts), graphics (public/images), stylesheets
(public/stylesheets), and HTML files (public).

In Ruby on Rails, what is an "asset"?

What's considered an "asset" in the Ruby on Rails universe?
Are user generated files, such as images uploaded by the user, considered assets? Where should they be stored in the standard file structure of a Rails project? Should they be kept clear of any asset related directories like:
app/assets
lib/assets
public/assets
Relevant: The Asset Pipeline
Generally asset is anything that browser loads after it gets the HTML page. Meaning javascript, css and any images. But as you pointed out there are two different image types in a rails project.
1) the images related to your css and layout design, those go under the app/assets/images
2) the images that your users upload, those normally go into the public/system or public/uploads folder depending on what you use to receive the uploads
The lib/assets (or sometimes vendor/assets) is where you supposed to place js/css/images related to your front-end design and provided by third party libs. Say if you want to pull in some css or a js framework, that were you should place it.
And finally public/assets is where rails will compile your layout assets from the app/assets and vendor/assets folders when you run rake assets:precompile task for production.
To have it short, your design stuff goes to app/assets, and the user uploads go into public/system
User-uploaded files are not part of assets, and should definitely be kept clear of asset-related folders. You should put them somewhere in your public directory. I put mine in public/uploads, which is a common convention. And then you should ignore those files in git (or whatever VCS you're using).
Assets are basically: javascript, stylesheets, fonts, and images which are part of the site design itself, not part of the user-uploaded content.

serving large media files from the assets folder in rails

I want to put some rather large sound files into my assets folder in rails.
/app/assets/sounds
--- file1.wav
----file2.wav
when calling them thru the following URL
http://localhost:3000/assets/file1.wav
Rails (3.2.x) the file will be "served" (somehow) but I can never play it in the browser.
If however I put those files into the public folder they will be served and can be played.
I assume this is related to the fact that the public folder isn't touched by rails but is served by rack (afaik). I found a solution to use send_file but is this really needed?

Where do app images get stored in Rails 3.1?

With the introduction of the new directory structure in 3.1 (i.e. app/assets/), should app images (logo, banner, icons, main bg, etc) still be stored in public/images or should they go into app/assets/images?
Someone mentioned:
I would be shocked if anything in
app/assets can be served publicly -
that wouldn't make sense from either a
security viewpoint or from honoring
the convention of the public
directory. Since you need these flash
files to be publicly accessible, you
should store them in public.
Valid point. Which brings me to the question:
Based on above understanding. What about the app images? If images such as logo, banner, main background, icons are considered public, why is there an images directory in app/assets?
Or should we still put these types of images in public/images. If so, what is the images dir in app/assets used for?
Note: I haven't looked at Rails 3.1 yet...
As far as I understood DHH's keynote, app/assets was introduced so you could structure your application in a better way. But there's nothing wrong with storing images in public/images. Thanks to Michiel for pointing out that public/images will no longer be with us in Rails 3.1!
The public folder will the replaced by the assets folder. That's the folder that your web server will point to. Files from app/assets, lib/assets or other places will be copied (images) or compiled (css, js) to assets.
That means that assets is now considered a build directory, and you shouldn't store anything there - you might even decline to put it under version control, and just have the server generate or copy the images and css and scripts when you deploy.
See also: http://blog.nodeta.com/2011/06/14/rails-3-1-asset-pipeline-in-the-real-world/

Should I disable mod_rails for images and stylesheets directories?

I had a rare error in my Rails application. A CSS file was referring to non existing image files. And missing PNG file was somehow mapped to a controller action. Fortunately the action wasn't changing DB. This seems to be not OK that missing PNG can trigger controller action.
So should I disable mod_rails for static asset directories? However I've never heard this is required for Rails apps.
It is definitely a good idea, since if you allow any kind of image upload the target destination is usually the asset directory. Normally the user can quite easily upload a php or ruby file instead, so disabling all mod_evil_script for these directories is a good idea in general.
You should be serving static assets directly via Apache anyway, because it's faster. Let Rails do what it's designed to do which is handle dynamic requests.

Resources