0

您好,我正在使用 carrievwave gem 为我的 rails 应用程序上传图像,该应用程序运行良好,但我无法访问那些上传的文件。我得到了错误的路径 url http://localhost/uploads/gallery/image_name/354340/1.jpg (这里 '354340' 是 Userid 而不是自动递增的 id)

当前路径应该是这样的http://doitfornow:3000/uploads/gallery/image_name/2/1.jpg

模型

class Gallery < ActiveRecord::Base
  attr_accessible :image_name, :name, :user_id,:image_name#,:remote_image_name_url
  mount_uploader :image_name, ImageUploader
  belongs_to :user, :class_name => "User", :foreign_key => "user_id"

end

看法

<%@gallery.each do |f|  %>
<tr>
    <td class="left"><%= f.Gname%></td>
    <!-- # u.avatar.url -->
    <td class="left"><%= image_tag f.image_name.url , :size => "50x50" %></td>
    <td><%= link_to 'Detail',"/articles/detail?id="+f.Gid.to_s  %></td>
    <td class="left" colspan="5"><label class="input-control checkbox"><%= check_box_tag 'user_ids[]', f.Gid, false %> <span class="helper"></span></label></td>
</tr>
<% end %> 

image_uploder.rb

 # encoding: utf-8

class ImageUploader < CarrierWave::Uploader::Base

  # Include RMagick or MiniMagick support:
  # include CarrierWave::RMagick
  #  include CarrierWave::jekyll-minimagick


  # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
  # include Sprockets::Helpers::RailsHelper
  # include Sprockets::Helpers::IsolatedHelper
  # Choose what kind of storage to use for this uploader:
  storage :file
  # storage :fog

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
 def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
 end


  # Provide a default URL as a default if there hasn't been a file uploaded:
  # def default_url
  #   # For Rails 3.1+ asset pipeline compatibility:
  #   # asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
  #
  #   "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  # end

  # Process files as they are uploaded:
  # process :scale => [200, 300]
  #
  # def scale(width, height)
    # # do something
  # end

  # Create different versions of your uploaded files:
  # version :thumb do
    # # process :resize_to_limit => [200, 200]
      # process :resize_to_fill => [50, 50]
   # # process :scale => [50, 50]
  # end

  # Add a white list of extensions which are allowed to be uploaded.
  # For images you might use something like this:
  def extension_white_list
   %w(jpg jpeg gif png)
  end

  # Override the filename of the uploaded files:
  # Avoid using model.id or version_name here, see uploader/store.rb for details.
  # def filename
  #   "something.jpg" if original_filename
  # end

end
4

1 回答 1

0

我有同样的问题。我通过为carrierwave创建一个模块来做到这一点,因为我有更多的上传者需要处理。我覆盖了“store_dir”方法并添加了“set_store_dir”方法。所以我可以将我的文件存储在我想要的地方。我在类中设置了store_dir,包括上传者希望它有帮助

    module Helper::Carrierwave
        def set_store_dir( dir )
          @storedir = dir
        end

        def store_dir
          @storedir
        end
    end
于 2013-03-06T10:34:15.497 回答