为什么link_to助手在 Rails 生产中显示错误的路径



在我的应用程序中,我正在使用Carrierwave的文件上传。我使用MiniMagick调整图像大小并将图像保存为大版本,如下所示:

version :large do
  resize_to_limit(100, 100)
end

在视图中,我称之为"大"版本:

<%= image_tag @user.avatar.url(:large) %>

在开发环境中,将显示图像并且路径正确:

<img src="/uploads/user/....">

但是在生产环境中,不会显示任何图像,因为它呈现了错误的路径(它在 appname 前面):

<img src="appname/uploads/user/....">

我使用带有Nginx,Unicorn,Capistrano,Ruby 2.0.0p353和Rails 4.0.2的Ubuntu服务器

nginx.conf:

upstream unicorn {
  server unix:/tmp/unicorn.appname.sock fail_timeout=0;
}
server {
  listen 80 default deferred;
  server_name appname.domain.com;
  root /home/deployer/apps/appname/current/public;
  location ~ ^/(assets)/  {
    root /home/deployer/apps/appname/current/public;
    gzip_static on; # to serve pre-gzipped version
    expires max;
    add_header Cache-Control public;
  }
  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn;
  }
  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

载波上传器:

class UserUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick
  storage :file
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end
  version :large do
    resize_to_limit(100, 100)
  end
end

最后,它正在工作。我不得不从 production.rb 中删除这一行(我最初放置它是为了服务多个 rails 应用程序):

  config.relative_url_root = "/appname"

最新更新