4

I am using imgkit to take snapshot of my webpage. I run:

RAILS_ENV=production bundle exec rake assets:precompile To precompile my assets.

All file of app/assets directory are compiled to public/assets application.css compiled as application-7a23a105125768e41d9d24aee4553615.css.

My controller code is:

  kit = IMGKit.new(render_to_string(:partial => 'form', :height => 200, :transparent => true, :quality => 10, :layout => false,:locals => {:project => @project}))
  #  t = kit.to_img(:png) 
  kit.stylesheets << "#{Rails.root.to_s}/public/assets/application.css"
  #file = kit.to_file(Rails.root + "public/pngs/" + "screenshot.png")
  file = kit.to_file(Rails.root + "public/assets/" + "screenshot.png")
  #send_file("#{Rails.root.to_s}/public/pngs/screenshot.png", :filename => "screenshot.png", :type => "image/png",:disposition => 'attachment',:streaming=> 'true')

I don't know how to resolve /public/assets/application.css not found error...

No such file or directory - public/assets/application.css

I am using https://github.com/csquared/IMGKit/issues/36 to get css and work in my snapshot

edits

def update
#@kit = IMGKit.new(render_to_string, width: 480, height: 800, :quality => 100)
  respond_to do |format|
   if @project.update(project_params)
    kit = IMGKit.new(render_to_string(:partial => 'form', :height => 200, :transparent => true, :quality => 10, :layout => false,:locals => {:project => @project}))
  #  t = kit.to_img(:png) 
  kit.stylesheets << "self.class.helpers.asset_path('application.css')"
    #file = kit.to_file(Rails.root + "public/pngs/" + "screenshot.png")
 file = kit.to_file(Rails.root + "public/assets/" + "screenshot.png")
 #send_file("#{Rails.root.to_s}/public/pngs/screenshot.png", :filename => "screenshot.png", :type => "image/png",:disposition => 'attachment',:streaming=> 'true')
    format.html { redirect_to root_path, notice: 'Flyer was successfully updated.' }
    format.json { render :show, status: :ok, locatioFlyern: @project }
  else
    format.html { render :edit }
    format.json { render json: @project.errors, status: :unprocessable_entity }
  end
 end
end
Robert
  • 10,403
  • 14
  • 67
  • 117
Anish Shah
  • 333
  • 1
  • 14

1 Answers1

5

You can use Sprockets::Rails::Helper#asset_digest_path, found here. Since you are in the controller, you have access to it with

self.class.helpers.asset_digest_path('application.css')
# => "application-7a23a105125768e41d9d24aee4553615.css"

Similarly, asset_path will yield the path to the application.css file

self.class.helpers.asset_path('application.css')
# => "/assets/application-7a23a105125768e41d9d24aee4553615.css"
hjing
  • 4,922
  • 1
  • 26
  • 29
  • it gives ERROR 'No such file or directory !!'bad luck – Anish Shah May 23 '14 at 06:52
  • What if you do it in production mode? `RAILS_ENV=production rake assets:precompile` and then `RAILS_ENV=production rails console` and in there, `ApplicationController.helpers.asset_path('application.css')`? Do you have an `application.css` file in your public/assets folder after compiling your assets? – hjing May 23 '14 at 12:54
  • application-7a23a105125768e41d9d24aee4553615.css file created instead of application.css as i am using rails 4 – Anish Shah May 26 '14 at 05:31
  • plz see my projects controller in Edited post!! – Anish Shah May 26 '14 at 06:19
  • 1
    If `"#{Rails.root}/public/assets/#{self.class.helpers.asset_digest_path('application.css')}"` gives you the correct path to your css file, then you should add `kit.stylesheets << "#{Rails.root}/public/assets/#{self.class.helpers.asset_digest_path('application.css')}"`. The reason why just `"self.class.helpers.asset_digest_path('application.css')"` doesn't work is because at that point it is simply a string, not the path to your `application.css` file. – hjing May 26 '14 at 06:40
  • let me try ones again!! – Anish Shah May 26 '14 at 06:43
  • Now problem is in this line file = kit.to_file(Rails.root + "public/assets/" + "screenshot.png")http://localhost:3000/assets/screenshot.png No such file or directory – Anish Shah May 26 '14 at 06:53
  • This is beyond the scope of your original question, but `Rails.root + "public/assets/" + "screenshot.png"` should translate to something along the lines of `/path/to/rails/public/assets/screenshot.png`, not `localhost:3000/assets/screenshot.png` so I don't think that particular line is the issue. – hjing May 26 '14 at 15:27
  • I am using Rails 4.2.6 and in my controller `self.class.helpers.asset_digest_path('application.css')` is returning nothing and `self.class.helpers.asset_path('application.css')` is returning `/application.css` and not the finger-printed file name lying under `/public/assets` folder which I generated using `rake assets:precompile`. I am working on a feature involving ImgKit and want to include my CSS and JS in context of development environment. How can I make this work? – Jignesh Gohel Aug 09 '16 at 22:42
  • @AnishShah can you please let me know how you resolved your issue of using the pre-compiled asset path in ImgKit? – Jignesh Gohel Aug 10 '16 at 06:57