3

Is there a was to call stylesheet_link_tag from the controller? I am creating a PDF file in memory and passing it along to an api call to another service. I am using PDFKit and it requires me to send the style sheet link to it. I am using Rails 3.1 and therefore need access to the asset pipeline through this method.

Thanks for the help!

user229044
  • 232,980
  • 40
  • 330
  • 338
lundie
  • 337
  • 4
  • 24

2 Answers2

4

You should be able to use this to access the stylesheet from your controller:

ActionController::Base.helpers.asset_path("stylesheet_i_want.css")

Paul Simpson
  • 2,504
  • 16
  • 28
  • Thanks for the response. Under normal circumstances I think this would work. My issue is that I am passing this link to a pdf processor that is separate from my rails process and it requires the actual filesystem link to the file. It looks like that asset_path method provides the link to the file without the added hash of the compiled/processed css file. Ie I am able to get it to work if i pass in "#{Rails.root.to_s}/public/assets/pdf/pdf_layout-86723cb5eb9aff943867df0b379440c2.css", but this hash can change. – lundie Jan 11 '12 at 22:24
  • Try using the [digestion gem](https://github.com/spohlenz/digestion) - it will let you disable the fingerprinting on specific files/paths. Preventing the stylesheet from getting fingerprinted should let you use the `{Rails.root.to_s}` method you mentioned. – Paul Simpson Jan 12 '12 at 01:10
  • I ended up just manually placing the css file in the public folder. I looked at the gem but for now it's only a single css file that I need so I'm thinking ill just do it on my own. Thanks for the help! – lundie Jan 12 '12 at 14:17
2

This question is closely related to this one: How does one reference compiled assets from the controller in Rails 3.1?

See my answer there, but, more briefly, you can access the Rails asset pipeline, which is managed by the Sockets library, from Rails.application.assets. That will be a Sprockets::Environment instance, documented at the Sprockets project. You can use it like this:

Rails.application.assets['application.css'].pathname  #=> "/home/username/project..."
Rails.application.assets['application.css'].to_s  #=> "html, body { ..."
Community
  • 1
  • 1
Dave Burt
  • 1,838
  • 19
  • 20