2014-09-11 70 views
0

我正在使用设计,并且我想将字段名字姓氏添加到sign_up表单。 创建相应的列之后,我尝试使用新表单进行sign_up,但数据库中新字段的值为零。使用设计的自定义用户字段

之后,我创造了这个控制器:

class RegistrationsController < Devise::RegistrationsController 

    private 

     def sign_up_params 
     params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation) 
     end 

     def account_update_params 
     params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :current_password) 
     end 
end 

但是我还是在列如first_name和last_name为零。 我错过了什么吗?

形式:

<h2>Sign up</h2> 

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> 
    <%= devise_error_messages! %> 

    <div><%= f.label :first_name %><br /> 
    <%= f.text_field :first_name %></div> 

    <div><%= f.label :last_name %><br /> 
    <%= f.text_field :last_name %></div> 

    <div><%= f.label :profile_name %><br /> 
    <%= f.text_field :profile_name %></div> 

    <div><%= f.label :email %><br /> 
    <%= f.email_field :email, autofocus: true %></div> 

    <div><%= f.label :password %> <% if @validatable %><i>(<%= @minimum_password_length %> characters minimum)</i><% end %><br /> 
    <%= f.password_field :password, autocomplete: "off" %></div> 

    <div><%= f.label :password_confirmation %><br /> 
    <%= f.password_field :password_confirmation, autocomplete: "off" %></div> 

    <div><%= f.submit "Sign up" %></div> 
<% end %> 

<%= render "devise/shared/links" %> 
+1

结帐这个海峡跟随文章http://www.jacopretorius.net/2014/03/adding-custom-fields-to-your-devise-user-model- in-rails-4.html – 2014-09-11 13:23:31

+1

http://stackoverflow.com/questions/16297797/add-custom-field-column-to-devise-with-rails-4 – yozzz 2014-09-11 13:23:57

回答

1

1个选项。 我想加入以下application_controller.rb将消除问题(而不是overridding的色器件的控制器):

private 

    def configure_devise_params 
    devise_parameter_sanitizer.for(:sign_up) do |u| 
     u.permit(:first_name, :last_name, :email, :password, :password_confirmation) 
    end 
    devise_parameter_sanitizer.for(:account_update) do |u| 
     u.permit(:first_name, :last_name, :email, :password, :password_confirmation) 
    end 
    end 

2选项。 检查过往的controllers: { registrations: 'devise/registrations' }块做devise_for :users线routes.rb

+1

这两种方式都是可能的! devise_parameter_sanitizer和重写。 – 2014-09-11 10:43:52

1

你你的路由文件中添加devise_for :users, :controllers => { registrations: 'devise/registrations' }?如果没有,则将其添加到您的路线文件中。其他东西看起来好吧

相关问题