【问题标题】:How to give fingerprint css path in production rails 4如何在生产rails 4中提供指纹css路径
【发布时间】:2014-07-07 08:27:24
【问题描述】:

我正在使用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 not found 错误...

没有这样的文件或目录 - 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

【问题讨论】:

  • 为什么要在开发环境中预编译?它不会对开发产生任何影响,你应该只在生产中这样做。
  • 如提到的github.com/csquared/IMGKit/issues/36我预编译了它!!
  • 谁能帮我克服这个问题
  • application-7a23a105125768e41d9d24aee4553615.cssapplication.css 不同。因此找不到。
  • @scones 我知道但我如何在生产中提供指纹路径

标签: ruby-on-rails heroku ruby-on-rails-4 precompile imgkit


【解决方案1】:

您可以使用Sprockets::Rails::Helper#asset_digest_path,找到here。由于您在控制器中,因此您可以访问它

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

同样,asset_path 将生成 application.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 文件吗?
  • application-7a23a105125768e41d9d24aee4553615.css 文件创建而不是 application.css,因为我使用的是 rails 4
  • 请在编辑后的帖子中查看我的项目控制器!!
  • 如果"#{Rails.root}/public/assets/#{self.class.helpers.asset_digest_path('application.css')}" 为您提供了css 文件的正确路径,那么您应该添加kit.stylesheets &lt;&lt; "#{Rails.root}/public/assets/#{self.class.helpers.asset_digest_path('application.css')}"。只是 "self.class.helpers.asset_digest_path('application.css')" 不起作用的原因是因为那时它只是一个字符串,而不是您的 application.css 文件的路径。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-12
  • 1970-01-01
  • 2016-07-30
  • 2013-07-16
  • 2014-03-08
  • 2014-07-04
  • 2014-12-29
相关资源
最近更新 更多