How to generate assets that match the html file of a rails application?

Tags: html,css,ruby-on-rails,ruby,erb

Problem :

I have created an html file by using render_to_string in a controller and send_data to send the html file to the user. That works great and the file I get back matches the html I see when I right click and view page source in my browser. However, in my html file there are links to css files and .js files that don't exist in my app/assets folder.

For example, there is a link to application.css in the html file but there is only application.css.scss in the app/assets folder. Or another example; lightbox.css.erb instead of lightbox.css.

Also, some of the paths in the html are wrong, for example images are located in app/assets/images, but the html is looking in app/assets. The pathing is not an issue since I can just edit the generated html with nokogiri, but I am curious as to why they are different.

This is pretty much exactly what I want to do rails: emulate "Save page as" behaviour

Except that I'm getting my html a different way and for whatever reason I can't find the assets I'm looking for. The html appears valid as I indicated earlier, and opening the file will display text from the html but won't display any images or assets until I change the link paths.

From what I've read, rails will transform scss to css and render erb files to js or html. I can't find these files anywhere in my directories, though. How can I generate the files that I need in order to display my static html file independently from the rails app?

I'm using Rails 3.2.2 with jruby 1.6.7 (ruby 1.9.2)

Solution :

Before a rails app is deployed to production mode, all of your assets (css, javascripts and images) are usually precompiled and placed in the public directory, which is where your rails-rendered html pages look for assets.

In development mode, this precompilation is not usually done, but it can be done if desired. To generate the compiled assets, you can run rake assets:precompile. This will compile your assets, "fingerprint" them and place them in the public folder. "Fingerprint" means that it will generate a hash based on the file content of an asset and place the hash in the asset's filename, which helps to determine if/when the content of the asset has changed just by reading the filename.

After you run this command, you can copy or move the assets to a new directory (outside your rails app) where you can serve the html pages and these assets independently from the rails app. You might also delete the compiled assets from the public directory, since these aren't generally very useful in development mode.

For some background, I would read about the Asset Pipeline.

    CSS Howto..

    how to make css for work for different mobile sites

    How do I script such that my panoramic image can be panned left and right up to its edges?

    How to place a color cover on top of an image in CSS

    How to replicate the styling of html heading tags

    How to create a button with a CSS-animated background?

    how do i get css source file path from firebug?

    How to apply gradient to the border of a div?

    how create triangle using css [duplicate]

    how to dynamically change css class for html form elements based on other css class

    How can I restrict the number of characters entered into an HTML Input with CSS (or jQuery, if necessary)?

    How to make css jQuery thumbnail slides loop

    How can I solve this :before or :after issue in IE7?

    How to properly align something that resizes in a specific location

    Learn how to arrange elements correctly using CSS [closed]

    How to apply custom CSS and Javascript specific CMS page Magento

    How can I adjust image width and height to 100 x 100 keeping ratio?

    How to unhecked parent checkbox

    CSS - How to make
    side by side not wrap when user make browser size changing?

    How to edit a div inside PHP using CSS

    How do I show a handle to indicate to the user that a panel is resizable?

    how to avoid extra blank page at end while printing?

    How to use a different stylesheet for iphone or android?

    How to make a button that can change background color of a doucment?

    How to set the background image of an DIV in CSS?

    How can I make my footer stay at the bottom with a dynamically re-sizable form?

    CSS Grow on hover adds padding to the bottom of an image, how do I stop this from happening?

    How to align text next to a div?

    How do I create a bar chart in CSS with an array of PHP float values?

    How to make a responsive div stick to page edge

    overlaying divs with onclick in slideshow -> onclick doesnt change