這是我的色器件註冊頁面定製: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_name
和last_name
,但我需要用戶在配置文件頁面註冊他們的first_name
和last_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
你創建領域的遷移工作,可能? – DickieBoy
@DickieBoy,您好我的first_name字段是在配置文件表不是用戶表。 – d3bug3r
你應該發佈一些更多的代碼。用戶模型和配置文件模型。 – DickieBoy