这是我对设计注册页面的自定义:
<%= 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&#39; 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 :(得分:2)
您可以在User模型中将first_name和last_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回调添加一个if块,因为如果您没有在用户模型上设置名称字段,它可能会删除配置文件信息:
after_save :save_profile, if: -> {first_name && last_name}
你可以在这里使用任何适合你的逻辑,可能是{first_name || last_name}
等。