How to give fingerprint css path in production rai

2019-01-25 21:09发布

问题:

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

回答1:

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"