我是新手,所以请原谅我不太好的解释。我正在使用宝石Devise。我已正确设置电子邮件和密码。我现在想添加一个用户名。所以我做的是:
rails g migration AddUserNameToUsers Username:string
invoke active_record
create db/migrate/20170203113948_add_user_name_to_users.rb
然后:
rails db:migrate
== 20170203113948 AddUserNameToUsers: migrating
-- add_column(:users, :Username, :string)
-> 0.0017s
== 20170203113948 AddUserNameToUsers: migrated (0.0024s)
然后我检查它是否已添加到users表中:
sqlite> select * from users;
13|Username|varchar|0||0
然后我设置了设计注册视图以包含用户名:
<div class="form-group">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :Username %><br />
<%= f.text_field :Username, autofocus: true, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :password %> <% if @validatable %><i>(<%= @minimum_password_length %> characters minimum)</i><% end %><br />
<%= f.password_field :password, autocomplete: "off", class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off", class: "form-control" %>
</div>
<div class="form-group">
<%= f.submit "Sign up", class: "btn btn-primary" %>
</div>
<% end %>
当我提交表单但未添加用户名时,页面返回时没有错误。日志显示:
Started POST "/users" for ::1 at 2017-02-03 12:43:40 +0000
Processing by Devise::RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"MfdDSafryl0mnHM3BzEtxkOoImZXUm7rfcq2sYrNaltD2S8i3SvFCRdMqam9ukEZXcY23zF/NpY/LPyj6tJm2A==", "user"=>{"email"=>"joe@test.com", "Username"=>"james", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
Unpermitted parameter: Username
答案 0 :(得分:1)
class ApplicationController < ActionController::Base
before_filter :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
end
end