设计如何在创建用户窗体中添加附加字段? [英] Devise how to add a addtional field to the create User form?

查看:171
本文介绍了设计如何在创建用户窗体中添加附加字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在设计/注册/新我有:

 < h2>注册< / h2> 

<%= form_for(resource,:as => resource_name,:url => registration_path(resource_name))do | f | %GT;
<%= devise_error_messages! %GT;
< 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注册%>< / p>
<%end%>

<%= render:partial => devise / shared / links%>

问题是没有 params [:username] 发送给控制器,我在视图中收到以下错误:

  ActiveRecord :: StatementInvalid in Devise :: RegistrationsController #create 

Mysql ::错误:列'username'不能为null:
INSERT INTO`users`(`email`,`encrypted_pa​​ssword`,'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
应用程序跟踪|框架跟踪|完整追踪

请求

参数:

{utf8=>✓,
authenticity_token=> qkQ8L0ZonXYxWQ2f4cfdREZ222oa2zGUb / qll3TRxjQ =,
user=> {username=>hansen,
email=>mail@test.dk,
password=>[FILTERED],
password_confirmation=>[FILTERED]},
commit=>注册}

我已经将用户名 coloumn添加到我的模型,但是如何访问控制器中的 params [:username]

解决方案

4将参数清理移动到控制器。



为设计添加自定义字段的一种方法是在应用程序控制器中添加一个过滤器,调用方法来定义哪些是您允许的参数。



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

  class ApplicationController< ActionController :: Base 
before_filter:configure_permitted_pa​​rameters,如果::devise_controller?

protected

def configure_permitted_pa​​rameters
devise_parameter_sanitizer.for(:sign_up)<< :username
end
end

上面的代码是如果你要添加字段命名用户名。如果你添加first_name,那将是:

  devise_parameter_sanitizer.for(:sign_up)<< :first_name 

这是一种方式,我强烈地考虑阅读上面链接的文档,以便详细了解定制设计以允许某些字段。


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$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 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.

这篇关于设计如何在创建用户窗体中添加附加字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆