Development: custom fields are not saved after registration

Environment: Rails 4, development 3.2.2
Considering that I added two fields (name, profession) to my development model.
When I write
Then I see that in the database the new columns are not affected.

Below are my control classes and models.

#custom controller
class UsersController < Devise::RegistrationsController
  before_action :configure_permitted_parameters

  #devise controller extensions

  protected

  def configure_permitted_parameters
     devise_parameter_sanitizer.for(:sign_up) do |u|
       u.permit(:name, :email, :profession, :password, :password_confirmation)
     end
  end
end

#Devise Model
class User < ActiveRecord::Base
   devise :database_authenticatable, :registerable,
          :recoverable, :rememberable, :trackable, :validatable
   attr_accessor :name, :profession
   validates :name, presence: true
   validates_uniqueness_of  :email, :name
end

      

I indicate that development is working correctly and using my custom controller.
I also checked in the hash of the parameter and it includes the new field values, as you can see:

Processing by UsersController#create as HTML
  Parameters: {"utf8"=>"✓",  "authenticity_token"=>"F3AjMd1/EpYvuhckEMPUkiMZ1szHa6ba7OMbjjSltOk=", "user"=>{"name"=>"test", "email"=>"you@contect.com", "profession"=>"prof", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Save"}

      

And below given sql:

INSERT INTO "users" ("created_at", "email", "encrypted_password", "updated_at") VALUES  (?, ?, ?, ?)  [["created_at", Tue, 11 Feb 2014 15:14:56 UTC +00:00], ["email", "you@contect.com"], ["encrypted_password", "$2a$10$Bn3LIldBUDPHKO0vrXd7peVz6q/42hOrCOcqdbvBMHVeEtn4EfKma"], ["updated_at", Tue, 11 Feb 2014 15:14:56 UTC +00:00]]

      

which does not contain any added fields.

Any hint would be welcome, I've been stuck for over one hour.

+1


source to share


1 answer


Remove this line from your model:

attr_accessor :name, :profession

      



This will override the accessories already provided by the active record after you migrate your model and add these fields. This makes them not write to the db.

+2


source







All Articles