2014-09-11 23 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 :usersroutes.rb

+1

這兩種方式都是可能的! devise_parameter_sanitizer和重寫。 – 2014-09-11 10:43:52

1

你你的路由文件中添加devise_for :users, :controllers => { registrations: 'devise/registrations' }?如果沒有,則將其添加到您的路線文件中。其他東西看起來好吧

相關問題