0
我試圖讓一個個人資料圖片顯示給用戶沒有運氣。我已經將寶石與mini_magick
一起安裝在gem文件中。Ruby on Rails image_tag不顯示圖像Carrierwave
圖片上傳
class AvatarUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :thumb do
process :resize_to_fit => [80, 80]
end
def extension_white_list
%w(jpg jpeg gif png)
end
user.rb
class User < ActiveRecord::Base
...
mount_uploader :avatar, AvatarUploader
end
show.html.erb
...
<%= image_tag @user.avatar.url(:thumb) if @user.avatar?%>
<%= link_to "Edit", edit_user_path(@user)%>
控制器(正在顯示alart,而不是圖像)
def update
if @user.update(user_params)
redirect_to @user
else
flash.now[:alert] = "Something went wrong. Please try again."
render :edit
end
end
private
def check_authorization
unless current_user.id == params[:id].to_i
redirect_to root_url
end
end
def set_user
@user = User.find(params[:id])
end
def user_params
params.require(:user).permit(:username, :avatar)
end
end