设计如何向创建用户表单添加附加字段? [英] 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" %>
问题是没有 params[:username]
发送到控制器,我在视图中收到以下错误:
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$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"}
我已将 username
列添加到我的模型中,但如何访问控制器中的 params[:username]
?
I have added the username
coloumn to my model, but how can I access the params[:username]
in my controller?
推荐答案
Rails 4 将 param sanitizing 移至控制器.
Rails 4 moved the param sanitizing to the controller.
为设计添加自定义字段的一种方法是在应用程序控制器中添加一个 before 过滤器,调用一个方法来定义哪些是您允许的参数.
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.
在代码中来自 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
上面的代码是如果您要添加一个名为 username 的字段.如果您要添加 first_name,它将是:
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.
这篇关于设计如何向创建用户表单添加附加字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!