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.
Related
For every version of my app, I upload my assets to the cloud CDN, and the images on the website are loaded as something like imagename-somehash.
I have static images that are large, i.e like carousel images, which don't change often or at all. So even when the app changes, they can remain cached by clients who previously visited the website.
How would one do that? Any images I have under app/assets folder gets upload to CDN with the imagename-somehash format, so the image changes with every version of the app.
I'm using rails 4.2. Is this possible?
In rails guides there is an article What is Fingerprinting and Why Should I Care? which gives some insights about that hash on the end of a file and how it works.
Check your environments files. In production you may want config.assets.digest to be true.
You can put these images in public folder and can mention full url in views for these images
OR You can also put it on
vendor/assets/images
but in that case you would have to disabled precompile path
If you want to serve only some assets from your CDN, you can use custom :host option your asset helper, which overwrites value set in config.action_controller.asset_host.
<%= asset_path 'image.png', host: 'mycdnsubdomain.fictional-cdn.com' %>
I am using Ruby on Rails 4.1.1 and I want to add a linked image - my app's logo - in email messages but I had a doubt since my previous question.
By using files located in the public directory it seems that to link to an image in your Asset Pipeline you can use
link_to(LINK_TEXT_OR_IMAGE_TAG_HELPER, image_path(IMAGE_NAME))
but for those located in the app/assets it seems do not work the same, at least in rendered email messages in production mode. All assets that are compiled in Production have a fingerprint ID... is the fingerprint causing the load of static assets to do not render images in email messages?
I doubt since I would like to access images from both browser and email. I tried to solve the issue by moving the image from app/assets/images/logo.png to public/images/logo.png and by changing statements in my application.css.scss file from image-url("logo.png") to url("/images/logos.png"). However I do not know if I am following "the Rails way" or a "best practice". Do I? Should I add to the public directory all assets that I plan to use outside my application and in the app/assets directory all assets that I plan to use internally to my application?
For emails, it almostisn't any different compared to standard Rails views.
You can link to an image in your mailer using the image_tag helper you'd normally use in regular views as well:
<%= image_tag('some_image.jpg') %>
You also need to tell Action Mailer where it can find the assets, because it will use absolute URLs to refer to your images:
config.action_mailer.asset_host = 'http://www.example.com/'
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 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?
I'm just getting into the Asset Pipeline; I'm using SASS/SCSS, but I'm not understanding why I should be using the Asset Helpers.
For example, if I have some CSS/SCSS without using an Asset Helper:
background-image: url('rails.png');
This will work fine because both my .SCSS file and image are in and accessible through the assets directory.
What's the point of doing this?:
background-image: asset-url("rails.png", image);
I understand it will add "/assets/" to the url, but why should I be using the Asset Helpers if the standard CSS way will work?
I think I'm missing something. Does it have something to do with deploying to production?
Using the helpers gives you access to the finger printed URLs in production. From the Asset Pipeline guide:
In the production environment Rails uses the fingerprinting scheme outlined above. By default it is assumed that assets have been precompiled and will be served as static assets by your web server.
During the precompilation phase an MD5 is generated from the contents of the compiled files, and inserted into the filenames as they are written to disc. These fingerprinted names are used by the Rails helpers in place of the manifest name.
So in production, the paths have an MD5 appended and you have things like this:
/assets/pancakes-af27b6a414e6da00003503148be9b409.png
With the checksums in place, Rails can tell browsers to cache these files forever. Then, if you do a new release that changes one of your assets, the checksum changes and that changes the whole path; the new path makes the browser think it is a whole new file so it will fetch it again. Without the checksums you can easily get old files stuck in browser caches and that sort of thing isn't exactly a happy fun time.