2

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)

Community
  • 1
  • 1
Spencer
  • 1,527
  • 1
  • 10
  • 23

1 Answers1

1

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.

cdesrosiers
  • 8,862
  • 2
  • 28
  • 33