【问题标题】:unknown attribute :avatar when uploading with image i am getting this error未知属性:头像上传图片时出现此错误
【发布时间】:2014-02-28 06:02:28
【问题描述】:

我在 15 和 99 的注册控制器中遇到错误 我已经在我的应用中完成了以下操作

def user_params
#assumption: user params are coming in params[:user]
params.require(:user).permit(:first_name, :last_name, :mobile, :uid, :provider, :avatar )
end
in user.rb

has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/

in migration

class AddAvatarToUsers < ActiveRecord::Migration
  def change

add_attachment :users, :avatar

  end
end

ActiveRecord::UnknownAttributeError in MyDevise::RegistrationsController#create

app/controllers/my_devise/registrations_controller.rb:99:in `build_resource'
app/controllers/my_devise/registrations_controller.rb:15:in `create'

提取的源代码(第 99 行附近):

def create
15.   self.resource = build_resource(sign_up_params)
if resource.save
  # yield resource if block_given?
  if resource.active_for_authentication?
    set_flash_message :notice, :signed_up if is_flashing_format?
    sign_up(resource_name, resource)
    respond_with resource, :location => after_sign_up_path_for(resource)
  else
    set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format?
    expire_data_after_sign_in!
    respond_with resource, :location => after_inactive_sign_up_path_for(resource)
  end
else
  clean_up_passwords resource
  respond_with resource
end
end

# temporary session data to the newly created user.
def build_resource(hash=nil)
99   self.resource = resource_class.new_with_session(hash || {}, session)
end

# Signs in a user on sign up. You can overwrite this method in your own

【问题讨论】:

  • 迁移成功了吗?

标签: ruby-on-rails activerecord paperclip


【解决方案1】:

Build

我认为您的问题与 Devise building 您的对象有关:

def build_resource(hash=nil)
    self.resource = resource_class.new_with_session(hash || {}, session)
end

您对 Devise 的操作感到困惑(最好别管 Devise 并在其上添加功能)


额外字段

这是我要做的:

  1. 设置 Devise 以处理额外的属性
  2. 添加额外属性注册后

Params

class ApplicationController < ActionController::Base
  before_filter :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) << :avatar
  end
end

字段

您可以在注册新用户时使用这些资源为您的模型添加额外的属性:

从外观上看,更改参数选择器,您应该可以通过注册表单上传avatar图像

【讨论】:

  • 很好,您可能想考虑接受我的回答;)
猜你喜欢
  • 2013-12-16
  • 2018-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-28
  • 2016-12-30
  • 2017-08-10
  • 2023-03-18
相关资源
最近更新 更多