回形针不保存在数据库
我使用下面的宝石:回形针不保存在数据库
'paperclip'
'aws-sdk', '~> 2.3'
我想将图像保存到S3,但我无法让他们保存。
模型/ 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
控制器/ 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("https://stackoverflow.com/users/#{@user.id}")
end
end
private
def user_params
params.require(:user).permit(:avatar, :name, :email, :phone_number, :description, :college_name)
end
end
为什么图像头像没有存储在数据库中? 我应该添加任何数据库列吗?我该怎么办?我将不胜感激任何意见来帮助我解决这个问题。
回形针必须配置为使用S3来存储对象(图像)。它将在数据库中存储与这些元数据相关的元数据,但不包括图像。
您还需要设置一个访问策略为您的S3存储桶,并在用户模型中定义它们是如何从S3解决。
谢谢,为我解答! –
我已经在config/application.yml中设置了我的S3存储桶,但是我没有在用户模型上定义,因为我不知道如何在用户模型上编写定义。我该怎么做 –
Paperclip Gem文档是要走的路。如果你看看S3的具体文档,它提供了一个操作方法和示例:[Paperclip S3 Documentation](http://www.rubydoc.info/gems/paperclip/Paperclip/Storage/S3) – michaeldever
尝试更新用户时是否收到任何特定的错误消息? – michaeldever
不,我不是。没有错误消息。 –