gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 07:08:02 25 4
gpt4 key购买 nike

嗨,我正在尝试使不同版本的默认图像适用于我的 avatar_uploader。我之前使用过 2 个版本,但不是我添加了 4 个版本,似乎有一个我无法弄清楚的错误。当我引用我的 :small 版本的默认图像时,它显示 :large 版本的默认值。我已经从我的 public/images/fallback 文件夹中删除了 large_avatar_defualt.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)

意见

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

用户.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

class AvatarUploader < CarrierWave::Uploader::Base
 # 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

最佳答案

这应该可以解决问题!

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

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

否则 CSS 就是你的 friend !

关于ruby-on-rails - rails 3 : carrierwave default_url bug, 没有使用正确的版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7941515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com