Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 回形针url以返回资产\u主机_Ruby On Rails_Ruby_Ruby On Rails 3_Paperclip - Fatal编程技术网

Ruby on rails 回形针url以返回资产\u主机

Ruby on rails 回形针url以返回资产\u主机,ruby-on-rails,ruby,ruby-on-rails-3,paperclip,Ruby On Rails,Ruby,Ruby On Rails 3,Paperclip,我正在寻找一种解决方案,以获得一个回形针对象的绝对url。url方法仅返回相对url。所以我试了一下: Paperclip::Attachment.default_options.update({ :url => "#{ActionController::Base.asset_host.call(nil, request)}/system/:attachment/:id/:style/:filename", :path => ":rails_root/public/syste

我正在寻找一种解决方案,以获得一个回形针对象的绝对url。url方法仅返回相对url。所以我试了一下:

Paperclip::Attachment.default_options.update({
  :url => "#{ActionController::Base.asset_host.call(nil, request)}/system/:attachment/:id/:style/:filename",
  :path => ":rails_root/public/system/:attachment/:id/:style/:filename"
})
但是初始值设定项中缺少请求。或者我如何得到它

我的资产\u主机配置如下所示:

ActionController::Base.asset_host = Proc.new do |source, request|
  if request.ssl?
    "#{request.protocol}#{request.host_with_port}"
  else
    "http://cdn.somehost.com"
  end
end
我受够了


谢谢你的时间

这是一个有点复杂的解决方案,但您可以这样做,首先使用before_过滤器设置一个变量,该变量将在请求是否为SSL时保持不变:

class ApplicationController < ActionController::Base

  before_filter :set_current_request
  after_filter :unset_current_request

  protected

  def set_current_request
    Thread.current[:current_request] = request
  end

  def unset_current_request
    Thread.current[:current_request] = nil
  end         

end
然后,您可以在配置中包含此插值:

Paperclip::Attachment.default_options.update({
  :url => ":assets_host/system/:attachment/:id/:style/:filename",
  :path => ":rails_root/public/system/:attachment/:id/:style/:filename"
})

我没有这样做,但我已经使用过很多次插值(S3存储也是这样做的,这很神奇),所以它应该可以工作。

这是一个有点复杂的解决方案,但您可以这样做,首先使用before_过滤器设置一个变量,该变量将在请求是否为SSL时保持不变:

class ApplicationController < ActionController::Base

  before_filter :set_current_request
  after_filter :unset_current_request

  protected

  def set_current_request
    Thread.current[:current_request] = request
  end

  def unset_current_request
    Thread.current[:current_request] = nil
  end         

end
然后,您可以在配置中包含此插值:

Paperclip::Attachment.default_options.update({
  :url => ":assets_host/system/:attachment/:id/:style/:filename",
  :path => ":rails_root/public/system/:attachment/:id/:style/:filename"
})

我并没有这样做,但我已经使用过很多次插值(这也是S3存储的神奇之处),所以它应该可以工作。

太棒了!我认为进出控制器时无需阻塞。相反,我使用
{request.protocol}{request.host_with_port}
创建了一个视图帮助程序,您需要一个变通方法,使其在非请求环境下工作,比如延迟电子邮件、后台作业等。只是想知道为什么需要线程变量而不是实例变量?太好了!我认为进出控制器时无需阻塞。相反,我使用
{request.protocol}{request.host_with_port}
创建了一个视图帮助程序,您需要一个变通方法,使其在非请求环境下工作,比如延迟的电子邮件、后台作业……只是想知道为什么需要线程变量而不是实例变量?