I experienced a similar issue when working on a Rails 6 application.
I had 2 models that were connected by a one-to-one relationship:
Admin Model
class Admin < ApplicationRecord
has_one :personal_info, dependent: :destroy
accepts_nested_attributes_for :personal_info, update_only: true, allow_destroy:
end
Personal Info Model
class PersonalInfo < ApplicationRecord
before_save :personal_info_capitalize
# This is an enum that can be found in app/models/concerns
include Gender
belongs_to :admin, optional: true
private
def personal_info_capitalize
# set the personal info details to capitalize letters
self.first_name = first_name.capitalize
self.last_name = last_name.capitalize
end
end
And for the Admin Registration and Sessions, I was using the Devise gem
AdminsController
class Admins::RegistrationsController < Devise::RegistrationsController
# before_action :configure_sign_up_params, only: [:create]
.
.
.
def configure_sign_up_params
devise_parameter_sanitizer.permit(:sign_up, keys:
[personal_info_attributes: [:first_name,
:last_name]])
end
end
However, when I try to create a new Admin, it throws the error:
NoMethodError (undefined method `capitalize' for nil:NilClass):
And when I look at my logs, I see the following:
Started POST "/admins" for 127.0.0.1 at 2020-08-20 12:09:19 +0100
Processing by Admins::RegistrationsController#create as HTML
Parameters: {"authenticity_token"=>"+RcJcpyO7mh+DmWOJ0IsymbuIascduIamDI+8RcUxWukL5B+aNAlKlgbqNzanW1EZIJ5WCs3bmV9OI+PLRI43g==", "admin"=>{"personal_info_attributes"=>{"first_name"=>"Promise", "last_name"=>"Chukwuenyem"}, "email"=>"admin@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign Up"}
Unpermitted parameter: :personal_info_attributes
Here's how I fixed it:
The issue was that the callback that will invoke the sign_up
parameters was commented out. So the personal_info attributes
weren't being permitted by the AdminsController.
I simply uncommented the configure_sign_up_params
before_action in the AdminsController in other to permit those parameters:
class Admins::RegistrationsController < Devise::RegistrationsController
before_action :configure_sign_up_params, only: [:create]
.
.
.
def configure_sign_up_params
devise_parameter_sanitizer.permit(:sign_up, keys:
[personal_info_attributes: [:first_name,
:last_name]])
end
end
That's all.
I hope this helps