新添加的列的数据不被保存到数据库 [英] Newly added column's data not being saved to DB
问题描述
我使用的设计进行用户身份验证在我的Rails应用程序4。最近我添加了两个新列到用户模式。他们是 FIRST_NAME
和姓氏。
然后我更新了这两个这些属性的领域登入形式。然而,无论是被保存到当我测试创建一个新的用户数据库。我想这可能是一个质量分配问题,但我没有收到任何类型的数据库错误。成功创建用户,但仅在原始属性(电子邮件,密码和password_confirmation)被存储。任何人都可以帮忙吗?
下面是code为我的方式:
< DIV CLASS =行>
<%=的form_for(资源:如=> RESOURCE_NAME,:URL => registration_path(RESOURCE_NAME))做| F | %GT;
&所述;%= devise_error_messages! %GT;
< DIV CLASS =小-10小胶印0柱> < DIV CLASS =行>
< DIV CLASS =小两列>
<%= f.label:FIRST_NAME,等级:正确的内联'%GT;
< / DIV>
< DIV CLASS =小4列>
<%= f.text_field:FIRST_NAME,ID:正确的标签'%GT;
< / DIV>
< DIV CLASS =小6列>
<! - 空 - >
< / DIV>
< / DIV> < DIV CLASS =行>
< DIV CLASS =小两列>
<%= f.label:姓氏,等级:正确的内联'%GT;
< / DIV>
< DIV CLASS =小4列>
<%= f.text_field:姓氏,ID:正确的标签'%GT;
< / DIV>
< DIV CLASS =小6列>
<! - 空 - >
< / DIV>
< / DIV> < DIV CLASS =行>
< DIV CLASS =小两列>
<%= f.label:电子邮件,等级:正确的内联'%GT;
< / DIV>
< DIV CLASS =小4列>
<%= f.email_field:电子邮件ID:正确的标签'%GT;
< / DIV>
< DIV CLASS =小6列>
<! - 空 - >
< / DIV>
< / DIV> < DIV CLASS =行>
< DIV CLASS =小两列>
<%= f.label:密码,等级:正确的内联'%GT;
< / DIV>
< DIV CLASS =小4列>
<%= f.password_field:密码,ID:正确的标签'%GT;
< / DIV>
< DIV CLASS =小6列>
<! - 空 - >
< / DIV>
< / DIV> < DIV CLASS =行>
< DIV CLASS =小两列>
<%= f.label:password_confirmation,等级:正确的内联'%GT;
< / DIV>
< DIV CLASS =小4列>
<%= f.password_field:password_confirmation,ID:正确的标签'%GT;
< / DIV>
< DIV CLASS =小6列>
<! - 空 - >
< / DIV>
< / DIV> < DIV CLASS =行>
< DIV CLASS =小两列>
<! - 占位符 - >
< / DIV>
< DIV CLASS =小10列>
<%= f.submit创建帐户类:小键%>
< / DIV>
< / DIV> < DIV CLASS =行>
< DIV CLASS =小两列>
<! - 占位符 - >
< / DIV>
< DIV CLASS =小10列>
<%=呈现设计/共享/链接%GT;
< / DIV>
< / DIV>
< / DIV> < DIV CLASS =小三栏>
<! - 这里没有什么 - >
< / DIV>
<%结束%GT;< / DIV>
和显示出的属性已被添加在我的模型信息从控制台:
用户(ID:整数,电子邮件:字符串,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:日期时间,FIRST_NAME:字符串,姓氏:字符串)
您需要让色器件知道这些新的参数现在允许按照Rails的4强大的参数。您可以添加以下到您的应用程序控制器:
类的ApplicationController< ActionController的基地::
before_action:configure_permitted_parameters,如果:devise_controller? 保护 高清configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up)LT;< :FIRST_NAME<< :姓
结束
结束
有关使用铁4强大的参数与设计更信息,请参阅强参数 README <第/ A>
I am using Devise for user authentication in my Rails 4 app. Recently I added two new columns to the User model. They are first_name
and last_name.
I then updated the signin form with fields for both of these attributes. However, neither are being saved to the database when I test creating a new user. I thought it might be a mass assignment problem but I'm not getting any kind of database error. The user is successfully created but only the original attributes (email, password, and password_confirmation) are being stored. Can anyone help?
Here is the code for my form:
<div class="row">
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="small-10 small-offset-0 columns">
<div class="row">
<div class="small-2 columns">
<%= f.label :first_name, class: 'right inline' %>
</div>
<div class="small-4 columns">
<%= f.text_field :first_name, id: 'right-label' %>
</div>
<div class="small-6 columns">
<!-- empty space -->
</div>
</div>
<div class="row">
<div class="small-2 columns">
<%= f.label :last_name, class: 'right inline' %>
</div>
<div class="small-4 columns">
<%= f.text_field :last_name, id: 'right-label' %>
</div>
<div class="small-6 columns">
<!-- empty space -->
</div>
</div>
<div class="row">
<div class="small-2 columns">
<%= f.label :email, class: 'right inline' %>
</div>
<div class="small-4 columns">
<%= f.email_field :email, id: 'right-label' %>
</div>
<div class="small-6 columns">
<!-- empty space -->
</div>
</div>
<div class="row">
<div class="small-2 columns">
<%= f.label :password, class: 'right inline' %>
</div>
<div class="small-4 columns">
<%= f.password_field :password, id: 'right-label' %>
</div>
<div class="small-6 columns">
<!-- empty space -->
</div>
</div>
<div class="row">
<div class="small-2 columns">
<%= f.label :password_confirmation, class: 'right inline' %>
</div>
<div class="small-4 columns">
<%= f.password_field :password_confirmation, id: 'right-label' %>
</div>
<div class="small-6 columns">
<!-- empty space -->
</div>
</div>
<div class="row">
<div class="small-2 columns">
<!--placeholder -->
</div>
<div class="small-10 columns">
<%= f.submit "Create account", class: 'button small' %>
</div>
</div>
<div class="row">
<div class="small-2 columns">
<!--placeholder -->
</div>
<div class="small-10 columns">
<%= render "devise/shared/links" %>
</div>
</div>
</div>
<div class="small-3 columns">
<!-- nothing here -->
</div>
<% end %>
</div>
And the info on my model from the console showing that the attributes have been added:
User(id: integer, email: string, encrypted_password: string, reset_password_token: string, reset_password_sent_at: datetime, remember_created_at: datetime, sign_in_count: integer, current_sign_in_at: datetime, last_sign_in_at: datetime, current_sign_in_ip: string, last_sign_in_ip: string, created_at: datetime, updated_at: datetime, first_name: string, last_name: string)
You need to let devise know that these new parameters are now permitted, in accordance with Rails 4's strong parameters. You can add the following to your application controller:
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :first_name << :last_name
end
end
For more information on using Rail 4's strong parameters with devise, see the Strong Parameters section of the Devise README.
这篇关于新添加的列的数据不被保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!