New fields should be nil, what to do?

1
I am using Devise, I do not want to use email for authentication, I want to use an enrollment, so I have read the Devise documentation and made the following adjustments, but the error still persists, it does not recognize the enrollment parameter when adding users.

Model User

class User < ApplicationRecord

attr_accessor :registration

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end

Controler Application

class ApplicationController < ActionController::Base
  attr_reader :current_user

private
  before_filter :configure_devise_params, if: :devise_controller?
  def configure_devise_params
    devise_parameter_sanitizer.for(:sign_up) do |u|
      u.permit(:registration, :email, :password,  :password_confirmation)
    end

  end

  protected
  def authenticate_request!
    unless user_id_in_token?
      render json: { errors: ['Not Authenticated'] }, status: :unauthorized
      return
    end
    @current_user = User.find(auth_token[:user_id])
  rescue JWT::VerificationError, JWT::DecodeError
    render json: { errors: ['Not Authenticated'] }, status: :unauthorized
  end

  private
  def http_token
      @http_token ||= if request.headers['Authorization'].present?
        request.headers['Authorization'].split(' ').last
      end
  end

  def auth_token
    @auth_token ||= JsonWebToken.decode(http_token)
  end

  def user_id_in_token?
    http_token && auth_token && auth_token[:user_id].to_i
  end
end

In my database I have already created the fields of registration , email , password and password_confirmation .

But when I try to create a user, however much I pass the data, it instances with null.

u = User.new(email:'[email protected]', registration:192536, password:'changeme', password_confirmation:'changeme')

=> #<User id: nil, email: "[email protected]", registration: nil>

Does anyone have an idea what it can be?

    
asked by anonymous 06.09.2016 / 22:56

1 answer

1

In your controller you must use a before_action to execute the method that will configure the parameters allowed by devise when creating the account. For example, to set the parameters allowed in creating and changing the account, see the code below:

 class ApplicationController < ActionController::Base

  before_action :configure_permitted_parameters, if: :devise_controller?

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

    devise_parameter_sanitizer.permit(:account_update) do |user_params|
      user_params.permit(:name, :password, :password_confirmation, :current_password)
    end
  end

end 
    
22.03.2018 / 06:25