Reputation: 2283
I've added my a username field to my User
model, but it seems that devise isn't recognizing it when creating a new user record. I fill out the email, username and password, but I get a validation error "Username can't be blank" even though my params hash clearly has it. I don't need to have people log in with their username, but I just need them to set it when registering for a new account.
I'm using rails 4 and I've implemented the strong parameters thing mentioned on devise's github README.
Here is my user.rb
file
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
before_save { |user| user.username = user.username.downcase }
validates_presence_of :username
validates_uniqueness_of :username, case_sensitive: false
Here's my application_controller.rb
file
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_filter :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :username, :password, :password) }
end
end
I've also added my registrations/new.html.erb
file:
<div><%= f.label :username %>
<%= f.text_field :username %></div>
Here's what my params hash has when I submit a new user record:
user: !ruby/hash:ActionController::Parameters
username: David
email: [email protected]
password: foobar12
password_confirmation: foobar12
Any ideas?
Upvotes: 0
Views: 1001
Reputation: 2283
As it turns out, my strong params above was incorrect.
I fixed it by changing it to this:
devise_parameter_sanitizer.for(:sign_up) do |u|
u.permit(:email, :username, :password, :password_confirmation)
end
Upvotes: 2