设计如何向创建用户表单添加附加字段?
我正在尝试在创建时向我的用户添加用户名。
在 devise/registrations/new 中,我有:
<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]
发送到控制器,我在视图中收到以下错误:
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 ('[email protected]',
'$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"=>"[email protected]",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]"},
"commit"=>"Sign up"}
我已添加 username< /code> 列到我的模型,但如何访问控制器中的
params[:username]
?
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 ('[email protected]',
'$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"=>"[email protected]",
"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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Rails 4 将参数清理移至控制器。
为设计添加自定义字段的一种方法是在应用程序控制器中添加一个 before 过滤器,调用一个方法来定义哪些是允许的参数。
的代码中
在 https://github.com/plataformatec/devise#strong-parameters 上面的代码是如果您要添加一个名为 username 的字段。如果您要添加first_name,则会是:
这是一种方法,我强烈考虑阅读上面链接中的文档,以了解有关自定义设备以允许某些字段的更多信息。
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
The above code is if you are adding a field named username. If you were adding first_name it would be:
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.
将用户名字段添加到 app/model/user.rb 中的 attr_accessible
取自上面的注释#1,以便其他人可以轻松查看解决方案
Add the username field to attr_accessible in app/model/user.rb
Taken from comment #1 above so others can easily see the solution
添加到控制器的 Rails 4 Strong Params 方式
https://github.com/plataformatec/devise#strong -参数
Rails 4 Strong Params way to add to the controller
https://github.com/plataformatec/devise#strong-parameters
如果您希望在用户注册时添加名字、姓氏或任何列,您必须使用设计允许的参数配置这些列。
例如:要在用户注册处添加一列(例如alternate_email),只需将alternate_email 列添加到registration_params
电子邮件]
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.
For example : To add one more column like alternate_email at User Registration, Just add alternate_email column to registration_params
email]