【问题标题】:Why does link_to helper show wrong path in Rails production?为什么 link_to 助手在 Rails 生产中显示错误的路径?
【发布时间】:2013-12-18 21:29:48
【问题描述】:

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

version :large do
  resize_to_limit(100, 100)
end

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

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

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

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

但在 production 环境中,不会显示任何图像,因为它呈现了错误的路径(它会添加应用程序名称):

<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

【问题讨论】:

  • 你的 nginx 配置是什么样的?您是否有一些机架中间件正在重写上传路径,或者 nginx 是否提供静态资产等...?
  • @cpjolicoeur 应该是默认设置,我添加了 nginx.conf
  • 你的carrierweave配置怎么样?
  • @cpjolicoeur 添加了载波配置。似乎在我的所有资产上,appname 都被添加到了路径中......

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


【解决方案1】:

终于,它开始工作了。我不得不从 production.rb 中删除这条线(我最初放置在那里是为了服务多个 Rails 应用程序):

  config.relative_url_root = "/appname"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-02
    • 1970-01-01
    • 2013-05-13
    相关资源
    最近更新 更多