1

I am getting error at in registrationscontroller at 15 and 99 I have done following in my app

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'

Extracted source (around line #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
Neels
  • 2,547
  • 6
  • 33
  • 40
venkatror
  • 29
  • 7

1 Answers1

0

Build

I think your problem is to do with Devise building your objects:

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

You're confusing Devise's operations (it's best to leave Devise alone & add functionality on top of it)


Extra Fields

Here's what I'd do:

  1. Set up Devise to handle extra attributes
  2. Add extra attributes after registration

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

Fields

You can use these resources to add extra attributes to your model when registering new users:

From the looks of it, changing the params selector, you should be able to upload the avatar image through the registration form

Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147