好吧,我仔细研究了一下,得出了以下解决方案
用户模型
# user.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me
belongs_to :rolable, :polymorphic => true
end
客户模式
# customer.rb
class Customer < ActiveRecord::Base
has_one :user, :as => :rolable
end
设计师模型
# designer.rb
class Designer < ActiveRecord::Base
has_one :user, :as => :rolable
end
因此,用户模型有一个简单的多态关联,定义它是客户还是设计师
然后我为新的注册定制了注册视图,在生成它们后,可以在app/views/devise/registrations/new.html.erb
中找到它们.
<h2>Sign up</h2>
<%
# customized code begin
params[:user][:user_type] ||= 'customer'
if ["customer", "designer"].include? params[:user][:user_type].downcase
child_class_name = params[:user][:user_type].downcase.camelize
user_type = params[:user][:user_type].downcase
else
child_class_name = "Customer"
user_type = "customer"
end
resource.rolable = child_class_name.constantize.new if resource.rolable.nil?
# customized code end
%>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= my_devise_error_messages! # customized code %>
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<% # customized code begin %>
<%= fields_for resource.rolable do |rf| %>
<% render :partial => "#{child_class_name.underscore}_fields", :locals => { :f => rf } %>
<% end %>
<%= hidden_field :user, :user_type, :value => user_type %>
<% # customized code end %>
<div><%= f.submit "Sign up" %></div>
<% end %>
<%= render :partial => "devise/shared/links" %>
For each User type I created a separate partial with the custom fields for that specific User type, i.e. Designer --> _designer_fields.html
<div><%= f.label :label_name %><br />
<%= f.text_field :label_name %></div>
然后我为Desive设置了注册时使用自定义控制器的路径
devise_for :users, :controllers => { :registrations => 'UserRegistrations' }
然后我生成了一个控制器来处理定制的注册过程,从Devise::RegistrationsController
中的create
方法复制了原始源代码,并对其进行了修改以适应我的工作方式(在我的例子app/views/user_registrations
中,不要忘记将视图文件移动到适当的文件夹中)
class UserRegistrationsController < Devise::RegistrationsController
def create
build_resource
# customized code begin
# crate a new child instance depending on the given user type
child_class = params[:user][:user_type].camelize.constantize
resource.rolable = child_class.new(params[child_class.to_s.underscore.to_sym])
# first check if child instance is valid
# cause if so and the parent instance is valid as well
# it's all being saved at once
valid = resource.valid?
valid = resource.rolable.valid? && valid
# customized code end
if valid && resource.save # customized code
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_navigational_format?
sign_in(resource_name, resource)
respond_with resource, :location => redirect_location(resource_name, resource)
else
set_flash_message :notice, :inactive_signed_up, :reason => inactive_reason(resource) if is_navigational_format?
expire_session_data_after_sign_in!
respond_with resource, :location => after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords(resource)
respond_with_navigational(resource) { render_with_scope :new }
end
end
end
这一切基本上都是这样做的,控制器根据视图中的隐藏字段传递给控制器create
方法的user_type
参数确定必须创建哪个用户类型,该字段通过URL中的简单GET参数使用该参数.
例如:
my_devise_error_messages!
方法是一种辅助方法,它基于原始devise_error_messages!
方法处理关联模型中的验证错误
module ApplicationHelper
def my_devise_error_messages!
return "" if resource.errors.empty? && resource.rolable.errors.empty?
messages = rolable_messages = ""
if !resource.errors.empty?
messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
end
if !resource.rolable.errors.empty?
rolable_messages = resource.rolable.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
end
messages = messages + rolable_messages
sentence = I18n.t("errors.messages.not_saved",
:count => resource.errors.count + resource.rolable.errors.count,
:resource => resource.class.model_name.human.downcase)
html = <<-HTML
<div id="error_explanation">
<h2>#{sentence}</h2>
<ul>#{messages}</ul>
</div>
HTML
html.html_safe
end
end
更新:
要支持/designer/sign_up
和/customer/sign_up
等路由,可以在路由文件中执行以下操作:
# routes.rb
match 'designer/sign_up' => 'user_registrations#new', :user => { :user_type => 'designer' }
match 'customer/sign_up' => 'user_registrations#new', :user => { :user_type => 'customer' }
路由语法内部未使用的任何参数都会被传递到params散列.所以:user
被传递给params散列.
所以就这样.我在这里和那里用了一点tweeking,使它以一种非常通用的方式工作,这很容易扩展到共享一个公共用户表的许多其他用户模型.
希望有人觉得有用.