如何在生产环境的Rails 4中提供指纹CSS路径

4

我正在使用 imgkit 来快照我的网页。我运行:

RAILS_ENV=production bundle exec rake assets:precompile 来预编译我的资产。

所有在 app/assets 目录的文件都被编译到 public/assets 中,application.css 被编译为 application-7a23a105125768e41d9d24aee4553615.css

我的控制器代码如下:

  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')

我不知道如何解决 /public/assets/application.css 未找到错误...

没有此文件或目录 - public/assets/application.css

我正在使用 https://github.com/csquared/IMGKit/issues/36 来获取CSS并在我的快照中工作。

编辑:

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

为什么在开发环境中预编译?这不会对开发产生任何影响,你应该只在生产环境中进行预编译。 - matanco
如 https://github.com/csquared/IMGKit/issues/36 所提到的,我已经预编译了它! - Anish Shah
有人可以帮我克服这个问题吗? - Anish Shah
“application-7a23a105125768e41d9d24aee4553615.css”与“application.css”不同,因此无法找到它。 - scones
@scones 我知道,但是在生产环境中如何提供指纹路径呢? - Anish Shah
1个回答

5

您可以使用Sprockets::Rails::Helper#asset_digest_path,此方法在这里找到。由于您在控制器中,因此可以通过以下方式访问它:

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

同样地,asset_path将产生应用程序.css文件的路径。
self.class.helpers.asset_path('application.css')
# => "/assets/application-7a23a105125768e41d9d24aee4553615.css"

如果您在生产模式下执行该操作会怎样呢?RAILS_ENV=production rake assets:precompile,然后 RAILS_ENV=production rails console,在控制台中输入 ApplicationController.helpers.asset_path('application.css')。在编译资产后,您的 public/assets 文件夹中是否有一个 application.css 文件? - hjing
1
如果"#{Rails.root}/public/assets/#{self.class.helpers.asset_digest_path('application.css')}"给出了正确的CSS文件路径,那么您应该添加kit.stylesheets << "#{Rails.root}/public/assets/#{self.class.helpers.asset_digest_path('application.css')}"。之所以仅使用"self.class.helpers.asset_digest_path('application.css')"不起作用是因为此时它只是一个字符串,而不是您的application.css文件的路径。 - hjing
让我再试一次!! - Anish Shah
我正在使用Rails 4.2.6,在我的控制器中,self.class.helpers.asset_digest_path('application.css')没有返回任何内容,而self.class.helpers.asset_path('application.css')返回的是/application.css,而不是位于/public/assets文件夹下的指纹文件名,这个文件名是我使用rake assets:precompile生成的。我正在开发一个涉及ImgKit的功能,并希望在开发环境中包含我的CSS和JS。我该如何解决这个问题? - Jignesh Gohel
@AnishShah,您能告诉我您是如何解决在ImgKit中使用预编译资产路径的问题的吗? - Jignesh Gohel
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接