سؤال

I got my production rails 3 app setup to use Fog/S3 for storage while running on Heroku. In the process, I made a few changes to /config/initializers/carrierwave.rb. Here's what it looks like:

CarrierWave.configure do |config|

  if Rails.env.test? || Rails.env.development?
    config.root = Rails.root
    config.storage = :file
  else
    config.storage = :fog
    config.fog_credentials = {
    :provider               => 'AWS',                        # required
    :aws_access_key_id      => ENV['MY_ID'],                        # required
    :aws_secret_access_key  => ENV['MY_KEY']                        # required
    }
      config.fog_directory  = 'my-app'                     # required
      config.fog_public     = false                                   # optional, defaults to true
      #config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}  # optional, defaults to {}
  end
end

Here's my image_uploader:

class ImageUploader < CarrierWave::Uploader::Base
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end
end

Uploads are working fine in production. Files are being uploaded to the correct destination in development, but I get this error when viewing an uploaded image:

ActionController::RoutingError (No route matches [GET] "/uploads/vendor/image/24/StoreB.png"):

I'm unsure of how to fix this. Am I missing something in my uploader? Help please? Let me know if you need more info. Thanks in advance!

EDIT Code example that throws error:

<% @stores.each do |s| %>
        <div class="row">
                <div class= "col-xs-9 horz-cent">
                        <%= link_to image_tag("#{s.vendor.image}"), user_show_store_path(s) %>
                </div>
        </div>
<% end %>
هل كانت مفيدة؟

المحلول

Set config.root as below

 config.root = Rails.root.join('public')

Currently the files are getting uploaded in Rails.root directory which are non accessible from the browser as they are not in public directory.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top