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?
Related
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).
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.
I have a question relating to how precompiled assets are utilized in a production environment. What about general file attachments as a part of the model? For example, I have the model "Event". You can have n attachments to the model, and they can be any file you want. Typically they are either image files or PDF files, but they can also be Excel files for example. These files are to be displayed as links to the user and the user can click the link to open the file. The attachment files are stored in the /assets directory in the following manner, alongside the standard assets:
/assets
/images
/javascripts
/stylesheets
/attachments
/events
/11
poster.jpg
event-details.pdf
Now as I understand it, when I run the precompile method, Sprockets generates gzipped/MD5'ed versions of the files to be served...how do I deal with these attachment files? If I run the precompile method, everything gets gzipped...but when I add/remove attachments further down the road through the web interface, some will be gzipped and others won't. What's the best way to deal with this?
I gave up trying to figure out a way around it and just set all the attachments as well as paperclip attachments to be physically put in the /public directory. From my vantage point, this removes the benefit of compressed assets, but whatever.
I'm trying to use an audio file in rails. I created a folder audios under app\assets\. I would like to use the assets precompile so that I don't have to put the file under app\public
Right now I'm getting
ActionController::RoutingError (No route matches [GET] "/audios/audio_file.wav")
If I change the url from URL/audios/audio_file.wav to URL/assets/audio_file.wav it works. How can I fix the problem? What is the right way?
First, in case you didn't realize it already: your new app/assets/audios folder is already in the load path... you just need to restart your server for Sprockets to pick it up.
In development, assets are available at the relative url: /assets/<asset file name>.
For example, assuming your wav file is located at /app/assets/audios/audio_file.wav in the filesystem, it would be accessible at the relative url /assets/audio_file.wav in the browser. This is because Sprockets/Dev-Rails knows to search the /app/assets folder and its subdirectories when locating assets.
In production, assets precompilation (typically) happens on deploy. At this time, your wav file is copied to e.g. /public/assets/audio_file-<MD5 fingerprint>.wav and is accessible at the relative url: /assets/audio_file-<MD5 fingerprint>.wav.
Because of the different naming styles used between development and production, any time you want to refer to an asset you should do so using a helper method (even in CSS!). That is, production includes the MD5 fingerprint, whereas development does not. But you don't have to worry about any of that so long as you use a helper:
For images: <%= image_tag('homes/logo.png') %> -- given an image file that lives in /app/assets/images/homes/logo.png on the file system.
For non-standard assets, such as audio files: <%= asset_path('audio_file.wav') %>, which would produce a relative path of /assets/audio_file.wav.
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/