class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
attr_accessor:login
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_one :user_profile ,dependent: :destroy #定义user与user_profile的一对一对关系
ACCESSABLE_ATTRS = [:userid, :email, :telno, :current_password, :password, :password_confirmation]
# VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
# validates :email, presence: true, length: { maximum: 255 },format: { with: VALID_EMAIL_REGEX },uniqueness: { case_sensitive: false }
# validates :userid,presence: true, length: { maximum: 255 },uniqueness: { case_sensitive: false }
# validates :telno,presence: true, length: { maximum: 14 }, uniqueness: true
before_save :ensure_authentication_token!
def self.find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
where(conditions.to_h).where(["lower(userid) = :value OR lower(email) = :value OR lower(telno) = :value", { :value => login.downcase }]).first
else
where(conditions.to_h).first
end
end
end
然后就是 user_profile
class UserProfile < ActiveRecord::Base
SEXTPYE = %w{男 女}
POSITION= %w{学生 辅导员 科目教师}
# validates :name,presence: true, length: { maximum: 80 }, uniqueness: true
mount_uploader :avatar, AvatarUploader
belongs_to :user
end
在判断 current_user 不为空的条件下用户完善子的 userprofile 调用 UserprofilesController 的 new 方法结果出现这个错误 这是我 userprofilescontroller 代码:
class UserProfilesController < ApplicationController
before_filter :authenticate_user!,only: [:show, :create, :edit, :update]
def show
@profile = UserProfile.find(params[:id])
end
def update
@profile = UserProfile.find(params[:id])
respond_to do |format|
if @profile.update_attributes(profile_params)
format.html { redirect_to root_path, notice: '已更新个人用户信息.' }
format.json { render :show, status: :ok, location: @profile }
else
format.html { render :edit }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
def new
@profile = UserProfile.new
end
def edit
@profile = current_user.user_profile
end
def create
@profile = current_user.build_user_profile(profile_params)
if @profile.save
flash[:success] = "用户信息已更新"
redirect_to root_path
else
render 'edit'
end
end
def destroy
@profile.destroy
rediret_to current_user
end
private
def profile_params
params.require(:user_profile).permit(:name,:sexuality,:age,:bloodtype,:class,:nickname,:mark,:position,:birthday,:avatar)
end
end