Can I share my CSS with another application when using asset pipeline? - ruby-on-rails

I am using Rails asset pipeline in a rails 4.x web app. In production I use a CDN (cloudfront) to serve the CSS.
My other application is a non-rails app, but it shares the same CSS as my Rails app.
Is there a way for my other application to use the CSS generated by my rails application?
The problem I am having is that since rails generates a random guid for the filename there is no way for me to reference it in my other application.
e.g.
https://abcd.cloudfront.net/assets/application-asdf23409usdflu34uasdf.css
Update
If I can get the value I can potentially expose the CSS URL as an API endpoint, is that possible?

When you deploying your application and precompiling assets, the task also generates a manifest-md5hash.json that contains a list with all your assets and their respective fingerprints. It looks like:
{"files":{"application-723d1be6cc741a3aabb1cec24276d681.js":{"logical_path":"application.js","mtime":"2013-07-26T22:55:03-07:00","size":302506, "digest":"723d1be6cc741a3aabb1cec24276d681"}, etc...}
You can transfer this file to another application and get correct filenames with guids from it.

Related

What can I serve from CDN for a Rails application?

I have a Ruby on Rails application where the views are contained in .html.erb files. If I introduce a CDN into the deployment pipeline, does that mean the CDN will only be able to serve up static assets such as JS, CSS and images? Will the html still be delivered from the Rails server? Serving the html from the Rails server seems like it will make page load times slower than if I could serve them from the CDN as well.
Thanks!

Heroku does not display images and also is not accepting my authentication User/Pass for access..How can I access my images?

I have deployed my exhisting project to Heroku. My main pages display some images, which have not loaded on heroku ..... And my backend end authentication pages , accessible only by me, will not accept my user/pass.
images are stored in
app/assets/images
Several points for you
Images
Heroku runs an ephemeral file system, which essentially means your files are overwritten each time you push a new deploy.
This doesn't matter for asset based images, but if you're using the likes of Paperclip to store dynamically-uploaded images, you need to ensure you're able to persist the data (with the likes of S3)
--
Precompilation
In your case, the problem will likely be to do with the precompilation process:
In the production environment Sprockets uses the fingerprinting scheme
outlined above. By default Rails assumes assets have been precompiled
and will be served as static assets by your web server.
Precompilation of the assets is when the Rails application will serve assets from the /public directory, rather than the /assets dir. This is super important as it means if you're referencing assets using "static" CSS references, it simply won't work, especially on Heroku
The best solution for this is to use one of the Rails preprocessors (SCSS / SASS) & use one of the asset path helpers to reference the dynamic asset location:
#app/assets/stylesheets/application.css.scss
body {
background: asset_url("your_image.png");
}
--
Authentication
Your authentication is probably a problem with your database
A problem with Heroku is that as it uses a different database than your local system, you'll have to populate it with the data required to get it working.
It's a common issue to not be able to load the database correctly, missing out many different migrations. I would recommend the following:
$ heroku run rake db:migrate
After this, you need to ensure that you're able to create the relevant details in your application

Rails 4 Asset Pipeline: Asset missing fingerprint in asset_path from js

I am deploying a Rails 4.0 application which includes HTML partial templates as assets for our front-end javascript framework. Although these templates are part of the asset pipeline and are properly precompiled, when I call asset_path from embedded ruby in our js files, it returns the path to our templates without the fingerprint.
I am quite certain that this is purely a Asset Pipeline question, but to give you a complete sense of our tech stack: We use Rails 4.0, Ruby 2.1, AngularJS for our front-end MVC framework, and AssetSync to synchronize our assets between Rails and our CDN.
An example of where this occurs (in a file included in app/assets/application.js.erb:
$routeProvider
.when('/', {
templateUrl: "<%= asset_path 'home.html' %>",
controller: "HomeController"
});
This works great locally, but as soon as config.assets.digest = true in production, the call to asset_path does not properly factor in the fingerprint. The templates are in the app/assets directory within a new subdirectory templates. So in the above example, the home.html asset is at app/assets/templates/home.html. Our javascript has itself been precompiled at that point, so I'm thinking that it might be an issue of which order the assets are precompiled in.
I've noticed a few issues on the Rails Github (1, 2, 3) and a couple of SO posts about fingerprints not being set properly (1, 2), but can't find anything about them not being included at all...
Any help or ideas that you can provide would be much appreciated.
Edit 4/15: forgot to include that the extensions on my application javascript file DOES include .erb (app/assets/application.js.erb). Thanks Alex for catching that. I've updated it above.
Also, following instructions in this article on Heroku, I confirmed that running puts helper.asset_path("home.html") from within a Rails console running in production prints a properly fingerprinted URL for that asset.
This appears to be an issue with the AssetSync gem. I removed it, reconfigured the app so that Rails serves the assets, and the fingerprinting works fine.
If anyone else finds this question and is running into the same issue, I would recommend against using AssetSync. According to Heroku:
Many developers make use of Amazon’s S3 service for serving static assets that
have been uploaded previously, either manually or by some form of build process.
Whilst this works, this is not recommended as S3 was designed as a file storage
service and not for optimal delivery of files under load. Therefore, serving
static assets from S3 is not recommended.
Amazon CloudFront is the preferred method of serving assets through a CDN, and is very easy to configure with a Rails app that serves its own static assets, accomplishing the same goals as AssetSync.
I'm pretty new to this stuff, but to get the asset_path to work, don't you need a .erb on the end of that file?
Check out the bottom of this article for more info:
https://devcenter.heroku.com/articles/rails-4-asset-pipeline
If it works in development, that may not help. There is a helpful section on debugging at the bottom of the article though.
Update
Here's another article that could help:
https://medium.com/self-directed-learning/9ba1f595102a
Flipping on this configuration in Heroku made some of my asset pipeline problems go away:
heroku labs:enable user-env-compile -a yourapp
Hope this helps!
Alex

Styling for multi tenant rails api apps

I have a rails-api app where I'd like to serve a stylus stylesheet with some variables set based on the current tenant.
The main app itself is a static, standalone RIA built in Dojo and doesn't make use of the Rails asset pipeline.
The stylesheet however, I'd like to have sprockets assemble from various sources as well as some values from the database.
Goals/desires:
I'd like to start out with a stylesheet in app/assets/stylesheets
I'd like the stylesheet in app/assets/stylesheets to include a base stylesheet that's distributed as part of the standalone RIA app found in public/ria/src/namespace/resources/base.styl
I'd like to be able to run the stylesheet in app/assets/stylesheets through ERB to populate it with some variables based on the current tenant
Ideally sprockets would cache the result of this process per-tenant
Important note:
I bootstrap the application with a simple static HTML file in /public. The RIA is in effect server agnostic and is a simple git module checked out into the public dir.

Page caching trick on Heroku?

I am moving a rails app to Heroku.
Heroku doesn't seem to support page caching.
So I generated cached pages on my development machine and checked them in to Heroku.
For example, /about_us generates public/about_us.html.
But when I call /about_us, public/about_us.html doesn't seem to be called.
Should my trick work?
Thanks.
Sam
In Rails 3, you'll be using the assets pipeline, so your assets--about_us.html--will be precompiled and put into a folder, WITHIN your public folder. Usually, this file will not be located at 'public/about_us.html'.
With your assets now precompiled, they'll be statically available and appended with an id, that will uniquely identify this asset until it is changed. With the unique signature, caching will occur on both Heroku's (last I checked) as well as within browsers.
Basically, the asset pipeline is doing this already for you.

Resources