2013-11-25 4 views
0

그래서 레일스 4를 사용하지만 protected_attributes .. 사용자를 만들 때 특정 속성 만 전자 메일 및 암호와 같은 DB에 저장합니다. 아래의 예를 참조하십시오 :사용자 데이터가 DB에 저장되지 않습니다

SELECT "users".* FROM "users" 
=> #<ActiveRecord::Relation [#<User id: 4, email: "[email protected]", encrypted_password: "$2a$10$x/dpzz6ED9xYwb8zn9dcBO2B/ODizHAu7vNtFwdiYzbT...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2013-11-25 22:34:18", last_sign_in_at: "2013-11-25 22:34:18", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2013-11-25 22:34:18", updated_at: "2013-11-25 22:34:18", zip: nil, gender: nil, first_name: nil, last_name: nil, birthday: nil>]> 

가 여기 내 new.html.erb의 당신 AS

class User < ActiveRecord::Base 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 

    attr_accessible :email, :password, :password_confirmation, :zip, :gender, :remember_me, :first_name, :last_name, :birthday 

end 

<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>                                                    
    <%= devise_error_messages! %>                                                                       
    <div><%= f.input :email %></div>                                                                    
    <div><%= f.input :password %></div>                              
    <div><%= f.input :password_confirmation %></div>                    
    <div><%= f.input :first_name %></div>                            
    <div><%= f.input :last_name %></div>                              
    <div><%= f.input :zip %></div>                                
    <div><%= f.label :birthday %></div>                               
    <%= f.date_select :birthday,                                 
    {:start_year => Time.now.year,                                
    :end_year => 1900,                                   
    :use_short_month => true,                                 
    :order => [:month, :day, :year],                                
    :prompt => {:month => 'Month', :day => 'Day', :year => 'Year'}},                        
    {:class => 'year',                                   
     :id => 'user_birthday'}                           %>                                        

     <label class="radio inline">                                 
     <div class="pushm"><%= f.radio_button "gender", "M" %>                          
     Male                                       
     </div>                                      
     </label>                                      
     <label class="radio inline">                                 
     <div class="pushf"> 
<%= f.radio_button "gender", "F" %>                               
    Female                                      
    </div>                                      
    </label>                                      
    <div><%= f.submit "Sign up" %></div><br />                                   
<%= render "devise/shared/links" %>                               
<% end %>                                                                          
</div> 

User.rb을 (물론 고안 사용) 성, 성별, 생일이 저장되지 않는 것을 볼 수 있습니다. 컨트롤러와

업데이트 :

class Users::RegistrationsController < Devise::RegistrationsController 

    # POST /resource                                    
    def create 
    build_resource 

    if resource.save 
     if resource.active_for_authentication? 
    set_flash_message :notice, :signed_up if is_navigational_format? 
    respond_with resource, :location => after_sign_up_path_for(resource) 
     else 
     set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format? 
    expire_session_data_after_sign_in! 
    respond_with resource, :location => after_inactive_sign_up_path_for(resource) 
     end 
    else 
     clean_up_passwords resource 
     respond_with resource 
    end 
    end 


    def after_sign_up_path_for(resource) 
    welcome_path 
    end 

end 
+0

당신은 당신의 컨트롤러 코드를 게시 할 수 있을까요? – kddeisz

+0

물론 원래 게시물에 추가되었습니다. –

+0

사용자를 만들 때 해당 필드에 값을 지정 했습니까? 유효성 검사를 수행하지 않으므로 필요하지 않습니다. – kddeisz

답변

1

application controller이 추가가 :

class ApplicationController < ActionController::Base 
     before_filter :configure_permitted_parameters, if: :devise_controller? 

     protected 

     def configure_permitted_parameters 
     devise_parameter_sanitizer.for(:sign_up) do |u| 
      u.permit(:email, :password, :password_confirmation, :current_password, 
      :first_name, :last_name, :your_others_attributes_allowed_on_signup) 
     end 
     end 
    end 
+0

감사합니다 rmagnum2002, 그게 효과가. –

관련 문제