【发布时间】:2018-01-18 20:20:54
【问题描述】:
我正在使用以下宝石:
'paperclip'
'aws-sdk', '~> 2.3'
我想将图像保存到 S3,但无法保存。
model/user.rb
class User < ApplicationRecord
# This method associates the attribute ":avatar" with a file attachment
has_attached_file :avatar, styles: {
thumb: '100x100>',
square: '200x200#',
medium: '300x300>'
}
# Validate the attached image is image/jpg, image/png, etc
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
end
迁移
class AddAvatarToUsers < ActiveRecord::Migration[5.1]
def self.up
add_attachment :users, :avatar
end
def self.down
remove_attachment :users, :avatar
end
end
controllers/users_controller.rb
class UsersController < ApplicationController
def edit
@user = User.find_by(id: params[:id])
end
def update
@user = User.find(params[:id])
if @user.update(user_params)
flash[:notice] = "Edit successfully"
redirect_to("/users/#{@user.id}")
end
end
private
def user_params
params.require(:user).permit(:avatar, :name, :email, :phone_number, :description, :college_name)
end
end
为什么图像头像没有存储在数据库中? 我应该添加任何数据库列吗?我该怎么办?如果有任何意见可以帮助我解决这个问题,我将不胜感激。
【问题讨论】:
-
您在尝试更新用户时是否收到任何特定的错误消息?
-
不,我不是。没有错误信息。
标签: ruby amazon-web-services heroku amazon-s3 ruby-on-rails-5