rails 3:carrierwave default_url bug,没有使用正确的版本

时间:2011-10-29 20:34:46

标签: ruby-on-rails ruby-on-rails-3 debugging carrierwave

您好我正在尝试使用不同版本的默认图片来为我的avatar_uploader工作。我之前有2个版本的工作,但不是我已经添加了4个版本,似乎是一个我无法弄清楚的错误。当我引用我的:小版本的默认图像时,它显示:大版本的默认值。我从public / images / fallback文件夹中删除了large_avatar_defaualt.png文件,并确认该请求确实在寻找large_avatar_default.png。正如您在下面的代码中所看到的,我指向正确的头像版本,任何想法为什么会发生这种情况?

Started GET "/users/example123/following" for 127.0.0.1 at 2011-10-29 13:16:31 -0700
  Processing by UsersController#following as JS
  Parameters: {"id"=>"example123"}
  User Load (62.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  User Load (3.5ms)  SELECT "users".* FROM "users" WHERE "users"."cached_slug" = 'example123' LIMIT 1
  User Load (2.1ms)  SELECT "users".* FROM "users" INNER JOIN "relationships" ON "users".id = "relationships".followed_id WHERE (("relationships".follower_id = 1)) LIMIT 30 OFFSET 0
Rendered users/_user.html.erb (3.5ms)
Rendered users/_users.html.erb (5.5ms)
Rendered users/following.js.erb (7.5ms)
Completed 200 OK in 312ms (Views: 25.2ms | ActiveRecord: 69.2ms)


Started GET "/images/fallback/large_avatar_default.png" for 127.0.0.1 at 2011-10-29 13:16:31 -0700

ActionController::RoutingError (No route matches "/images/fallback/large_avatar_default.png"):


Rendered /Users/iHal/.rvm/gems/ruby-1.9.2-p290@rails3/gems/actionpack-3.0.7/lib/action_dispatch/mid

dleware/templates/rescues/routing_error.erb within rescues/layout (1.1ms)

视图

users.html.erb

<% if users.empty? %>
<%= @empty_msg %>
<% else %>
<%= @title %>
<% users.each do |user| %>
<%= render 'user', :user => user %>
<% end%>
<% end%>

user.html.erb

<div>
  <%= small_avatar_for user %>
  <%= link_to user.name, user %>
  <%= link_to user.username, user %>
</div>

users_helper.rb

module UsersHelper

  def tiny_avatar_for(user)
     image_tag(user.avatar_url(:tiny).to_s, :alt => "avatar", :class => "round-3")
   end

  def small_avatar_for(user)
    image_tag(user.avatar_url(:small).to_s, :alt => "avatar", :class => "round-3")
  end

   def medium_avatar_for(user)
       image_tag(user.avatar_url(:medium).to_s, :alt => "avatar", :id => "align-middle", :class => "round-3")
     end

  def large_avatar_for(user)
     image_tag(user.avatar_url(:large).to_s, :alt => "avatar", :class => "round-10", :id => "center-big-avatar")
   end
end

类AvatarUploader&lt; CarrierWave ::上传::基

 # Include RMagick or ImageScience support:
  include CarrierWave::MiniMagick
  # include CarrierWave::ImageScience

  # Choose what kind of storage to use for this uploader:
  #storage :file
  # storage :s3

  # 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
   "/images/fallback/" + [tiny, "tiny_avatar_default.png"].compact.join('_')
   "/images/fallback/" + [small, "small_avatar_default.png"].compact.join('_')
   "/images/fallback/" + [medium, "medium_avatar_default.png"].compact.join('_')
   "/images/fallback/" + [large, "large_avatar_default.png"].compact.join('_')
  end

  # Process files as they are uploaded:
  # process :scale => [200, 300]
  process :resize_to_fit => [600, 600]

  # def scale(width, height)
  #   # do something
  # end

  # Create different versions of your uploaded files:

  version :large do
    process :resize_to_fill => [165, 165]
  end
  version :medium do
    process :resize_to_fill => [75, 75]
  end
  version :small do
    process :resize_to_fill => [45, 45]
  end
  version :tiny do
    process :resize_to_fill => [20, 20]
  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:
  def filename
    "avatar.#{File.extname(original_filename).downcase}" if original_filename
  end
  def filename
    if Rails.env.production?
      name_s3
    else
      name_file
    end
  end

  def name_s3
    # S3 storage class is Carrierwave::Storage::S3::File
    "avatar.#{File.extname(original_filename).downcase}" if original_filename     
  end

  def name_file
    # :file storage class is Carrierwave::Storage::File
    "avatar.#{model.avatar.file.extension}" if original_filename
  end
end

2 个答案:

答案 0 :(得分:14)

这应该可以解决问题!

def default_url
  "/images/fallback/" + [version_name, "avatar_default.png"].compact.join('_')
end

version_name将传递所请求版本的名称。

否则CSS就是你的朋友!

答案 1 :(得分:3)

答案在于您的代码:

# Provide a default URL as a default if there hasn't been a file uploaded:
def default_url
  "/images/fallback/" + [tiny, "tiny_avatar_default.png"].compact.join('_')
  "/images/fallback/" + [small, "small_avatar_default.png"].compact.join('_')
  "/images/fallback/" + [medium, "medium_avatar_default.png"].compact.join('_')
  "/images/fallback/" + [large, "large_avatar_default.png"].compact.join('_')
end

default_url只返回一个值:最后一个。记住:它只是一个Ruby方法。