设计如何向创建用户表单添加附加字段?

发布于 2024-11-07 08:37:44 字数 2205 浏览 2 评论 0原文

我正在尝试在创建时向我的用户添加用户名。

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

喜爱纠缠 2024-11-14 08:37:44

Rails 4 将参数清理移至控制器。

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

的代码中

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

https://github.com/plataformatec/devise#strong-parameters 上面的代码是如果您要添加一个名为 username 的字段。如果您要添加first_name,则会是:

devise_parameter_sanitizer.for(:sign_up) << :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

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.

海之角 2024-11-14 08:37:44

将用户名字段添加到 app/model/user.rb 中的 attr_accessible

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

取自上面的注释#1,以便其他人可以轻松查看解决方案

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

沉溺在你眼里的海 2024-11-14 08:37:44

添加到控制器的 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

凉风有信 2024-11-14 08:37:44

如果您希望在用户注册时添加名字、姓氏或任何列,您必须使用设计允许的参数配置这些列。

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

例如:要在用户注册处添加一列(例如alternate_email),只需将alternate_email 列添加到registration_params

registration_params = [:名字,:姓氏,:电子邮件,:密码,
:password_confirmation, :alternate_email ]

电子邮件]

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]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文