这是创建的:
class AddAttachmentAvatarToProfiles < ActiveRecord::Migration
def self.up
change_table :profiles do |t|
t.attachment :avatar
end
end
def self.down
remove_attachment :profiles, :avatar
end
end
然后添加:
<%= f.label :avatar %>
<%= f.file_field :avatar, :autofocus => true, class: 'form-control' %>
我有这个def
profiles_controller
private
def profile_params
params.require(:profile).permit(:first_name, :last_name, :avatar, :phone_number, :contact_email, :description)
end
我在我的
application_controller
也是:
def configure_permitted_parameters
devise_parameter_sanitizer.for(:user) << :avatar
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:avatar) }
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:avatar) }
end
和
users_controller
def user_params
params.require(:profile).permit(:avatar)
end
在这之后仍然出现错误。
感谢您的帮助。
谢谢
["id", "user_id", "first_name", "last_name", "phone_number",
"contact_email", "description", "avatar_file_name",
"avatar_content_type", "avatar_file_size", "avatar_updated_at"]
@Pavan-我确实运行了迁移
更新:
class Profile < ActiveRecord::Base
belongs_to :user
class User < ActiveRecord::Base
has_attached_file :avatar, styles: { medium: "300x300>", thumb:
"100x100>" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :avatar, content_type:
/\Aimage\/.*\z/
end
end