CarrierWave/MiniMagick 头像不显示
CarrierWave/MiniMagick avatar not displaying
嘿,所以今天早上我在使用 CarrierWave 和 MiniMagick 时遇到了一些问题,上传的头像图像无法正确显示,正如您在这张截图中突出显示的那样,我在 html 和控制器周围进行了挖掘并且可以'我找不到问题所在,所以我会 post 在这里查看所有内容,看看第二组眼睛是否能够找到问题。
这是 AvatarUploader < CarrierWave::Uploader::Base:
所有这些参数都是从我在 bloc 的课程中复制的:
# Process files as they are uploaded:
# process :scale => [200, 300]
process :resize_to_fill => [200, 300]
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
version :tiny do
process resize_to_fill: [20, 20]
end
version :small do
process resize_to_fill: [30, 30]
end
version :profile do
process resize_to_fill: [45, 45]
end
这是来自 application.html.erb
的片段
Note: I am also using devise
<% if current_user %>
<%= image_tag(current_user.avatar.tiny.url) if current_user.avatar? %> Hello <%= link_to (current_user.name || current_user.email), edit_user_registration_path %>!
<%= link_to "Sign Out", destroy_user_session_path, method: :delete %>
<% else %>
<%= link_to "Sign In", new_user_session_path %> or <%= link_to "Sign Up", new_user_registration_path %>
<% end %>
最后是我的用户控制器:
class UsersController < ApplicationController
before_action :authenticate_user!
def update
if current_user.update_attributes(user_params)
flash[:notice] = "User information updated"
redirect_to edit_user_registration_path
else
flash[:error] = "Invalid user information"
redirect_to edit_user_registration_path
end
end
private
def user_params
params.require(:user).permit(:name, :avatar)
end
end
如果您需要查看更多文件,请询问,我会添加它们。
这个问题的答案是您应该使用
image_tag current_user.avatar.url(:tiny)
嘿,所以今天早上我在使用 CarrierWave 和 MiniMagick 时遇到了一些问题,上传的头像图像无法正确显示,正如您在这张截图中突出显示的那样,我在 html 和控制器周围进行了挖掘并且可以'我找不到问题所在,所以我会 post 在这里查看所有内容,看看第二组眼睛是否能够找到问题。
这是 AvatarUploader < CarrierWave::Uploader::Base:
所有这些参数都是从我在 bloc 的课程中复制的:
# Process files as they are uploaded:
# process :scale => [200, 300]
process :resize_to_fill => [200, 300]
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
version :tiny do
process resize_to_fill: [20, 20]
end
version :small do
process resize_to_fill: [30, 30]
end
version :profile do
process resize_to_fill: [45, 45]
end
这是来自 application.html.erb
的片段Note: I am also using devise
<% if current_user %>
<%= image_tag(current_user.avatar.tiny.url) if current_user.avatar? %> Hello <%= link_to (current_user.name || current_user.email), edit_user_registration_path %>!
<%= link_to "Sign Out", destroy_user_session_path, method: :delete %>
<% else %>
<%= link_to "Sign In", new_user_session_path %> or <%= link_to "Sign Up", new_user_registration_path %>
<% end %>
最后是我的用户控制器:
class UsersController < ApplicationController
before_action :authenticate_user!
def update
if current_user.update_attributes(user_params)
flash[:notice] = "User information updated"
redirect_to edit_user_registration_path
else
flash[:error] = "Invalid user information"
redirect_to edit_user_registration_path
end
end
private
def user_params
params.require(:user).permit(:name, :avatar)
end
end
如果您需要查看更多文件,请询问,我会添加它们。
这个问题的答案是您应该使用
image_tag current_user.avatar.url(:tiny)