我使用下面的寶石:回形針不保存在數據庫
'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
爲什麼圖像頭像沒有存儲在數據庫中? 我應該添加任何數據庫列嗎?我該怎麼辦?我將不勝感激任何意見來幫助我解決這個問題。
嘗試更新用戶時是否收到任何特定的錯誤消息? – michaeldever
不,我不是。沒有錯誤消息。 –