Rails重写如何将此代码重写为ruby代码?

时间:2012-04-01 10:32:21

标签: ruby-on-rails ruby ruby-on-rails-3

我有一个这类中间件:

class RedirectIt
require "net/https"
require "uri"
require 'open-uri'

  APP_DOMAIN = 'http://www.mydomain.com'
  def initialize(app)
    @app = app
  end

  def call(env)
        request = Rack::Request.new(env)
        response = Rack::Response.new(env)
        response.headers['Cache-Control'] = "public, max-age=#{84.hours.to_i}"
        response.headers['Content-Type'] = 'image/png'
        response.headers['Content-Disposition'] = 'inline'
        response.body = "#{open('http://s3-eu-west-1.amazonaws.com/bucket/asdas.png').read}"
  end

end

问题只是它给出了错误:

Started GET "/?view=boks" for 127.0.0.1 at 2012-04-01 04:07:58 +0200

NoMethodError (You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]=):

我做错了吗?我试图重写我在控制器中的代码:

def image_proxy
  image_url = "http://s3-eu-west-1.amazonaws.com/bucket#{request.path}"
  response.headers['Cache-Control'] = "public, max-age=#{84.hours.to_i}"
  response.headers['Content-Type'] = 'image/png'
  response.headers['Content-Disposition'] = 'inline'
  render :text => open(image_url, "rb").read
end

1 个答案:

答案 0 :(得分:0)

解决方案。

    #PROXY BILLEDER
    status, headers, response = @app.call(env)
    headers['Cache-Control'] = "public, max-age=#{84.hours.to_i}"
    headers['Content-Type'] = 'image/png'
    headers['Content-Disposition'] = 'inline'
    response_body = "#{(open('http://s3-eu-west-1.amazonaws.com/mybucket#{request.path()}')).read}"
    [status, headers, response_body]
相关问题