简体   繁体   中英

Why does link_to helper show wrong path in Rails production?

In my app I'm using file-upload with Carrierwave. I do image resizing with MiniMagick and save the image as large version like this:

version :large do
  resize_to_limit(100, 100)
end

In the view I call this "large" version:

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

In development env, the image shows up and the path is correct:

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

But in production env, no image gets displayed because it renders the wrong path (it prepends the appname):

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

I use an Ubuntu server with Nginx, Unicorn, Capistrano, Ruby 2.0.0p353 and Rails 4.0.2

The 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;
}

The carrierwave uploader:

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

Finally, it's working. I had to remove this line from production.rb (which I initially placed there for serving multiple rails apps):

  config.relative_url_root = "/appname"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM