How do I create an additional field to create a custom form?
I am trying to add a username to my user on creation.
In development / registration / new me:
<h2>Sign up</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<p><%= f.label :username %><br />
<%= f.text_field :username %></p>
<p><%= f.label :email %><br />
<%= f.email_field :email %></p>
<p><%= f.label :password %><br />
<%= f.password_field :password %></p>
<p><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></p>
<p><%= f.submit "Sign up" %></p>
<% end %>
<%= render :partial => "devise/shared/links" %>
The problem is that there is no dispatch to the controller params[:username]
and I see the following error in the view:
ActiveRecord::StatementInvalid in Devise::RegistrationsController#create
Mysql::Error: Column 'username' cannot be null:
INSERT INTO `users` (`email`, `encrypted_password`, `reset_password_token`,
`reset_password_sent_at`, `remember_created_at`, `sign_in_count`,
`current_sign_in_at`, `last_sign_in_at`, `current_sign_in_ip`, `last_sign_in_ip`,
`created_at`, `updated_at`, `username`) VALUES ('mail@test.dk',
'$2a$10$bWjAXLY8QGXrXeVrGciv2O6mjRF940lajBEsUOPPtPDhKyj0A/gia', NULL, NULL,
NULL, 0, NULL, NULL, NULL, NULL, '2011-05-15 16:16:36', '2011-05-15 16:16:36',
NULL)
Rails.root: C:/Rails/densjove
Application Trace | Framework Trace | Full Trace
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"qkQ8L0ZonXYxWQ2f4cfdREZ222oa2zGUb/qll3TRxjQ=",
"user"=>{"username"=>"hansen",
"email"=>"mail@test.dk",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]"},
"commit"=>"Sign up"}
I have added username
coloumn to my model, but how can I access it params[:username]
in my controller?
source to share
Rails 4 moved the parameter deactivating the controller.
One way to add custom fields for development is to add a before filter to the Application Controller that calls a method to determine what your allowed options are.
In your code from https://github.com/plataformatec/devise#strong-parameters
class ApplicationController < ActionController::Base
before_filter :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :username
end
end
The above code is if you add a field with username. If you add first_name it will be:
devise_parameter_sanitizer.for(:sign_up) << :first_name
This is one way, and I highly recommend reading the docs at the link above to learn more about configuring your device to allow certain fields.
source to share
If you want to add FirstName, LastName, or any column during user registration, you need to customize those columns with the allowed options.
class ApplicationController < ActionController::Base
before_action :configure_new_column_to_devise_permitted_parameters, if: :devise_controller?
protected
def configure_new_column_to_devise_permitted_parameters
registration_params = [:first_name, :last_name, :email, :password, :password_confirmation]
if params[:action] == 'create'
devise_parameter_sanitizer.for(:sign_up) {
|u| u.permit(registration_params)
}
elsif params[:action] == 'update'
devise_parameter_sanitizer.for(:account_update) {
|u| u.permit(registration_params << :current_password)
}
end
end
end
For example: To add another column like alternate_email on user registration, just add alternate_email column to registration_params
registration_params = [: first_name ,: last_name ,: email ,: password ,: password_confirmation ,: alternate_email]
E-mail address]
source to share