Newly added column's data not being saved to DB

匿名 (未验证) 提交于 2019-12-03 02:22:01

问题:

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)  

回答1:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!