未知属性:使用图片上传时的头像我收到此错误

时间:2014-02-28 06:02:29

标签: ruby-on-rails activerecord paperclip

我在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

1 个答案:

答案 0 :(得分:0)

Build

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

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

你对Devise的操作感到困惑(最好不要让Devise单独使用并在其上添加功能)


额外字段

这就是我要做的事情:

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

    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
    

    <强>字段

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

    从它的外观来看,更改params选择器,您应该能够通过注册表格上传avatar图像