2015-12-21 78 views
0

這是我的色器件註冊頁面定製:Rails的增加額外的Fileds

<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: {:class => 'text-left'}) do |f| %> 
      <div class="form-group"> 
      <%= f.label :first_name, 'First Name' %><br /> 
      <%= f.email_field :first_name, autofocus: true, placeholder: "First Name", class: "form-control" %> 
      </div> 

      <div class="form-group"> 
      <%= f.label :last_name, 'Last Name' %><br /> 
      <%= f.email_field :last_name, autofocus: true, placeholder: "Last Name", class: "form-control" %> 
      </div> 

      <div class="form-group"> 
      <%= f.label :email, 'Email' %><br /> 
        <%= f.email_field :email, autofocus: true, placeholder: "Email", class: "form-control" %> 
      </div> 
      <div class="form-group"> 
      <%= f.label :password %> 
      <%= f.password_field :password, autocomplete: "off", placeholder: "Password", class: "form-control" %> 
      </div> 

      <div class="form-group"> 
      <%= f.label :password_confirmation, 'Confirm Password' %> 
      <%= f.password_field :password_confirmation, autocomplete: "off", placeholder: "Confirm Password", class: "form-control" %> 
      </div> 
      <button type="submit" class="btn btn-default signup-btn">Submit</button> 
     <% end %> 

和之後都是我registration_controller.rb

class RegistrationsController < Devise::RegistrationsController 

    def create 
     @user = User.build(user_params) 
     if @user.sign_up 
      sign_in_and_redirect(:user, @user) 
     end 

     logger.info "event=custome registration is successful #{@user}" 
    end 


    def new 
     super 
    end 

    private 

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

end 

的問題是,在我的User模型我沒有一個領域first_namelast_name,但我需要用戶在配置文件頁面註冊他們的first_namelast_name。我該如何做到這一點,因爲當我註冊新用戶時,我收到一個錯誤undefined method first_name'for#`。由於

編輯

用戶模型:

class User < ActiveRecord::Base 
    # Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:facebook] 


    has_one :profile 
    delegate :id, to: :person, prefix: true 

    has_many :products 


    def self.init 
    Rails.logger.info("HERE IS INSIDE SELF INIT....") 
    end 

    def self.from_omniauth(auth) 

     where(provider: auth.provider, uid: auth.uid).first_or_create do |user| 
     user.email = auth.info.email 
     user.password = Devise.friendly_token[0,20] 
     #user.name = auth.info.name # assuming the user model has a name 
     #user.image = auth.info.image # assuming the user model has an image 
     end 
    end 

    def self.build(opts = {}) 
    u = User.new(opts.except(:profile, :id)) 
    u.setup(opts) 
    u 
    end 

    def setup(opts) 
    self.email = opts[:email] 
    self.valid? 
    self.set_profile(Profile.new((opts[:profile] || {}).except(:id))) 
    self 
    end 

    def set_profile(profile) 
    logger.info "PROFILE #{profile}" 
    self.profile = profile 
    end 

    def self.new_with_session(params, session) 
    super.tap do |user| 
     if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"] 
     user.email = data["email"] if user.email.blank? 
     end 
    end 
    end 

    def sign_up 
     save 
    end 


end 

剖面模型:

class Profile < ActiveRecord::Base 

    belongs_to :user 
end 
+0

你創建領域的遷移工作,可能? – DickieBoy

+0

@DickieBoy,您好我的first_name字段是在配置文件表不是用戶表。 – d3bug3r

+0

你應該發佈一些更多的代碼。用戶模型和配置文件模型。 – DickieBoy

回答

2

可以聲明FIRST_NAME和姓氏作爲您的用戶模型的非持久化屬性,如:

attr_accessor :first_name, :last_name 

然後聲明after_save的回調函數,像這樣:

after_save :save_profile 

def save_profile 
    profile.update(first_name:first_name, last_name:last_name) 
end 

編輯:您可能要包括,如果塊爲after_save的回調,因爲它可能消滅個人資料信息,如果你還沒有上設置名稱字段中輸入您用戶模型:

after_save :save_profile, if: -> {first_name && last_name} 

你可以用任何邏輯這裏爲你{first_name || last_name}

+0

嗨,我得到錯誤的參數數量(1爲2)'app/models/user.rb:43:在'save_profile'' – d3bug3r

+1

這不應該發生has_one關係,但它可能取決於您正在使用的Rails版本(?)。如果在該方法中將更新更改爲update_all,會發生什麼情況? – Joeman29

+0

現在工作。謝謝 – d3bug3r