Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access stylesheet_link_tag from controller

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!

like image 345
lundie Avatar asked Nov 07 '25 16:11

lundie


2 Answers

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

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

like image 53
Paul Simpson Avatar answered Nov 09 '25 14:11

Paul Simpson


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 { ..."
like image 30
Dave Burt Avatar answered Nov 09 '25 12:11

Dave Burt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!