为什么link_to helper在Rails生成中显示错误的路径?

时间:2013-12-18 21:29:48

标签: ruby-on-rails ruby capistrano carrierwave minimagick

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

version :large do
  resize_to_limit(100, 100)
end

在视图中我称之为“大”版本:

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

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

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

但是在制作 env中,没有显示图片,因为呈现错误的路径(它会在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

1 个答案:

答案 0 :(得分:0)

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

  config.relative_url_root = "/appname"