用户数据未保存在DB中

时间:2013-11-25 22:40:33

标签: ruby-on-rails ruby ruby-on-rails-4

所以,我正在使用rails 4但是使用protected_attributes ..出于某种原因,当我创建一个用户时,只有某些属性保存到数据库,如电子邮件和密码。 请参阅以下示例:

SELECT "users".* FROM "users"
 => #<ActiveRecord::Relation [#<User id: 4, email: "random.test@gmail.com", 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(当然是使用设计)

 <%= 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 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

如你所见,拉链,性别,生日都没有保存。

使用控制器更新:

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

1 个答案:

答案 0 :(得分: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