新添加的列的数据未保存到数据库 [英] Newly added column's data not being saved to DB

查看:26
本文介绍了新添加的列的数据未保存到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Rails 4 应用程序中使用 Devise 进行用户身份验证.最近我向 User 模型添加了两个新列.它们是 first_namelast_name. 然后我用这两个属性的字段更新了登录表单.但是,当我测试创建新用户时,两者都没有保存到数据库中.我认为这可能是一个批量分配问题,但我没有收到任何类型的数据库错误.用户已成功创建,但仅存储原始属性(电子邮件、密码和 password_confirmation).任何人都可以帮忙吗?

这是我的表单的代码:

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f|%><%= devise_error_messages!%><div class="small-10 small-offset-0 列"><div class="row"><div class="small-2 columns"><%= f.label :first_name, class: 'right inline' %>

<div class="small-4 columns"><%= f.text_field :first_name, id: 'right-label' %>

<div class="small-6 columns"><!-- 空白区域-->

<div class="row"><div class="small-2 columns"><%= f.label :last_name, class: 'right inline' %>

<div class="small-4 columns"><%= f.text_field :last_name, id: 'right-label' %>

<div class="small-6 columns"><!-- 空白区域-->

<div class="row"><div class="small-2 columns"><%= f.label :email, class: 'right inline' %>

<div class="small-4 columns"><%= f.email_field :email, id: 'right-label' %>

<div class="small-6 columns"><!-- 空白区域-->

<div class="row"><div class="small-2 columns"><%= f.label :password, class: 'right inline' %>

<div class="small-4 columns"><%= f.password_field :password, id: 'right-label' %>

<div class="small-6 columns"><!-- 空白区域-->

<div class="row"><div class="small-2 columns"><%= f.label :password_confirmation, class: 'right inline' %>

<div class="small-4 columns"><%= f.password_field :password_confirmation, id: 'right-label' %>

<div class="small-6 columns"><!-- 空白区域-->

<div class="row"><div class="small-2 columns"><!--占位符-->

<div class="small-10 列"><%= f.submit "Create account", class: 'button small' %>

<div class="row"><div class="small-2 columns"><!--占位符-->

<div class="small-10 列"><%= 渲染设计/共享/链接"%>

<div class="small-3 columns"><!-- 这里什么都没有-->

<%结束%>

来自控制台的关于我的模型的信息显示已添加属性:

User(id: integer, email: string, encrypted_pa​​ssword: 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字符串,created_at:日期时间,updated_at:日期时间,first_name:字符串,last_name:字符串)

解决方案

根据 Rails 4 的强参数,您需要让设计知道这些新参数现在是允许的.您可以将以下内容添加到您的应用程序控制器:

class ApplicationController <动作控制器::基础before_action :configure_permitted_pa​​rameters, if: :devise_controller?受保护def configure_permitted_pa​​rametersdevise_parameter_sanitizer.for(:sign_up) <<:first_name <<:姓结尾结尾

有关在设计中使用 Rail 4 的强参数的更多信息,请参阅设计自述文件强参数部分/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屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆