开发者

Devise how to add a addtional field to the create User form?

I am trying to add a username to my User on create.

In devise/registrations/new I have:

<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 there is no params[:username] sent to the controller and I get 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$bWjAXLY8QGXrXeVrGciv2O6mjRF940lajBEsUOPPtPDh开发者_如何学CKyj0A/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 the username coloumn to my model, but how can I access the params[:username] in my controller?


Rails 4 moved the param sanitizing to the controller.

One way to add custom fields for devise is to add a before filter in the Application Controller calling a method to define which are your permitted parameters.

In 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 are adding a field named username. If you were adding first_name it would be:

devise_parameter_sanitizer.for(:sign_up) << :first_name

This is one way and I strongly consider reading over the docs at the link above in order to learn more about customizing devise to permit certain fields.


Add the username field to attr_accessible in app/model/user.rb

# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :username

Taken from comment #1 above so others can easily see the solution


Rails 4 Strong Params way to add to the controller

https://github.com/plataformatec/devise#strong-parameters


If you are wishing to add FirstName, LastName or any column while Registration of User, You have to configure those column with devise permitted parameters.

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 one more column like alternate_email at User Registration, Just add alternate_email column to registration_params

registration_params = [:first_name, :last_name, :email, :password, :password_confirmation, :alternate_email ]

email]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜