使用 Carrierwave 上传图像时,不会将字符串隐式转换为哈希
No implicit conversion of String into Hash when uploading an image with Carrierwave
我是 Carrierwave 的新手,我正在使用它来允许我的用户上传头像。我尝试将其配置为使用 Fog 和 Amazon S3。每当我尝试更新我的用户并传入一个新的图像文件时,我都会收到类型错误,提示它无法将字符串转换为哈希。
载波初始化器
CarrierWave.configure do |config|
config.fog_provider = 'fog/aws'
config.fog_credentials = {
# Configuration for Amazon S3 should be made available through an Environment variable.
# For local installations, export the env variable through the shell OR
# if using Passenger, set an Apache environment variable.
#
# In Heroku, follow http://devcenter.heroku.com/articles/config-vars
#
# $ heroku config:add S3_KEY=your_s3_access_key S3_SECRET=your_s3_secret S3_REGION=eu-west-1 S3_ASSET_URL=http://assets.example.com/ S3_BUCKET_NAME=s3_bucket/folder
# Configuration for Amazon S3
:provider => 'AWS',
:aws_access_key_id => ENV['S3_KEY'],
:aws_secret_access_key => ENV['S3_SECRET'],
:region => ENV['S3_REGION']
}
config.fog_directory = ENV['S3_BUCKET_NAME']
config.fog_public = false
config.fog_attributes =
# For testing, upload files to local `tmp` folder.
if Rails.env.development?
config.storage = :file
config.enable_processing = false
config.root = "#{Rails.root}/tmp"
else
config.storage = :fog
end
config.cache_dir = "#{Rails.root}/tmp/uploads" # To let CarrierWave work on heroku
end
头像上传器
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
# include CarrierWave::MiniMagick
# 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
end
用户模型
attr_accessor :activation_token, :reset_token, :avatar
before_save :downcase_email
before_create :create_activation_digest
mount_uploaderuser :avatar, AvatarUploader
has_secure_password
用户#编辑视图
<div class="col-sm-6 offset-md-3">
<h2>Edit your profile</h2>
<%= form_for @user, :url => user_url(@user.username), :html => {:multipart => true} do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.text_field :email, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :avatar %>
<%= f.file_field :avatar, class: 'form-control-file' %>
</div>
<div class="form-group">
<%= f.label :password, "New Password" %>
<%= f.password_field :password, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :password_confirmation, "Confirm New Password" %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
</div>
<%= f.submit 'Update Profile', class: 'btn btn-primary' %>
<% end %>
</div>
用户控制器
skip_before_action :require_login, :only => [:new, :create]
before_action :correct_user, only: [:edit, :update]
def update
@user = User.find_by_username(params[:username])
if @user.update_attributes(user_params)
flash[:success] = "User updated"
redirect_to user_url(current_user.username)
else
render "edit"
end
end
private
def user_params
params.require(:user).permit(:name, :username, :email, :password, :password_confirmation, :avatar)
end
# Confirms current user
def correct_user
@user = User.find_by_username(params[:username])
if current_user?(@user)
else
flash[:danger] = "Can't edit someone else's profile"
redirect_to user_url(current_user.username)
end
end
end
提前感谢您提供的任何帮助!
这一行等号后没有任何内容..
config.fog_attributes =
...所以它将下一行分配给 config.fog_attributes 这(正在开发中)意味着它将分配字符串 "#{Rails.root}/tmp"
注释掉该行。
我是 Carrierwave 的新手,我正在使用它来允许我的用户上传头像。我尝试将其配置为使用 Fog 和 Amazon S3。每当我尝试更新我的用户并传入一个新的图像文件时,我都会收到类型错误,提示它无法将字符串转换为哈希。
载波初始化器
CarrierWave.configure do |config|
config.fog_provider = 'fog/aws'
config.fog_credentials = {
# Configuration for Amazon S3 should be made available through an Environment variable.
# For local installations, export the env variable through the shell OR
# if using Passenger, set an Apache environment variable.
#
# In Heroku, follow http://devcenter.heroku.com/articles/config-vars
#
# $ heroku config:add S3_KEY=your_s3_access_key S3_SECRET=your_s3_secret S3_REGION=eu-west-1 S3_ASSET_URL=http://assets.example.com/ S3_BUCKET_NAME=s3_bucket/folder
# Configuration for Amazon S3
:provider => 'AWS',
:aws_access_key_id => ENV['S3_KEY'],
:aws_secret_access_key => ENV['S3_SECRET'],
:region => ENV['S3_REGION']
}
config.fog_directory = ENV['S3_BUCKET_NAME']
config.fog_public = false
config.fog_attributes =
# For testing, upload files to local `tmp` folder.
if Rails.env.development?
config.storage = :file
config.enable_processing = false
config.root = "#{Rails.root}/tmp"
else
config.storage = :fog
end
config.cache_dir = "#{Rails.root}/tmp/uploads" # To let CarrierWave work on heroku
end
头像上传器
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
# include CarrierWave::MiniMagick
# 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
end
用户模型
attr_accessor :activation_token, :reset_token, :avatar
before_save :downcase_email
before_create :create_activation_digest
mount_uploaderuser :avatar, AvatarUploader
has_secure_password
用户#编辑视图
<div class="col-sm-6 offset-md-3">
<h2>Edit your profile</h2>
<%= form_for @user, :url => user_url(@user.username), :html => {:multipart => true} do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.text_field :email, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :avatar %>
<%= f.file_field :avatar, class: 'form-control-file' %>
</div>
<div class="form-group">
<%= f.label :password, "New Password" %>
<%= f.password_field :password, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :password_confirmation, "Confirm New Password" %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
</div>
<%= f.submit 'Update Profile', class: 'btn btn-primary' %>
<% end %>
</div>
用户控制器
skip_before_action :require_login, :only => [:new, :create]
before_action :correct_user, only: [:edit, :update]
def update
@user = User.find_by_username(params[:username])
if @user.update_attributes(user_params)
flash[:success] = "User updated"
redirect_to user_url(current_user.username)
else
render "edit"
end
end
private
def user_params
params.require(:user).permit(:name, :username, :email, :password, :password_confirmation, :avatar)
end
# Confirms current user
def correct_user
@user = User.find_by_username(params[:username])
if current_user?(@user)
else
flash[:danger] = "Can't edit someone else's profile"
redirect_to user_url(current_user.username)
end
end
end
提前感谢您提供的任何帮助!
这一行等号后没有任何内容..
config.fog_attributes =
...所以它将下一行分配给 config.fog_attributes 这(正在开发中)意味着它将分配字符串 "#{Rails.root}/tmp"
注释掉该行。