Ruby on rails 4 如何实现具有多个配置文件类型的1个用户模型
如何为1个用户模型实现多个配置文件类型 例如: 用户注册帐户。无论是在注册过程中,还是在编辑默认配置文件时,他们都可以选择、粉丝、乐队、场地、推广人 当前默认用户模型收集:配置文件名、电子邮件、密码。用户注册后,可以使用城市、州和zipcode编辑其帐户 以下是我当前配置文件控制器的一个示例:Ruby on rails 4 如何实现具有多个配置文件类型的1个用户模型,ruby-on-rails-4,Ruby On Rails 4,如何为1个用户模型实现多个配置文件类型 例如: 用户注册帐户。无论是在注册过程中,还是在编辑默认配置文件时,他们都可以选择、粉丝、乐队、场地、推广人 当前默认用户模型收集:配置文件名、电子邮件、密码。用户注册后,可以使用城市、州和zipcode编辑其帐户 以下是我当前配置文件控制器的一个示例: class ProfilesController < ApplicationController def show @user = User.find_by_profile(params
class ProfilesController < ApplicationController
def show
@user = User.find_by_profile(params[:id])
if @user
@gigs = @user.gigs.all
render action: :show
else
render file: 'public/404', status: 404, formats: [:html]
end
end
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
# Sends email to user when user is created.
GigittMailer.gigitt_email(@user).deliver
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
end
models/user.rb:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
belongs_to :role
before_create :set_default_role
private
def set_default_role
self.role ||= Role.find_by_name('user')
end
before_create :capitalize_names
def capitalize_names
self.profile = profile.camelcase
end
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates :profile, presence: true,
uniqueness: true,
format: {
with: /[a-zA-Z0-9-]+/,
message: 'Must be formated correctly.'
}
has_many :gigs
end
我不了解1个用户模型的多种配置文件类型。。。你的意思是你希望能够指定一个用户实例既是一个乐队又是一个推广人吗?没错。保留一个用户模型,因为大多数数据都相同,但有多个配置文件类型。除非您认为拥有多个用户类型会更好/更容易?