Rails中设计的单表继承4

cba*_*ock 10 ruby-on-rails single-table-inheritance devise ruby-on-rails-4

我已经在这里,这里这里阅读了帖子,但是我仍然无法实现单表继承.

理想情况下,我希望有两个注册路径(一个用于客户端,一个用于提供者),具有公共字段名称,电子邮件,密码和confirm_password,以及提供者注册具有额外的radiobutton字段以指定提供者类型.我正在通过设计进行注册.在点击注册表单上的提交后,用户将被重定向到第二种形式,这对于客户端和提供者来说是完全不同的(我一直在使用资源的编辑页面).

按照目前的情况,如果我只是通过用户进行操作,一切正常,但只要我添加单表继承,注册表单就会告诉我他们缺少第二种表单的要求.

这是我的 config/routes.rb

Rails.application.routes.draw do
    devise_for :users, :controllers => {:sessions => "sessions"}, :skip=> :registrations
    devise_for :clients, :providers, :skip=> :sessions
    resources :clients
    resources :providers
    root :to=>'pages#home'
    match '/home', to: 'pages#home', via: 'get'
end
Run Code Online (Sandbox Code Playgroud)

我的模型如下:

用户:

class User < ActiveRecord::Base

    before_save {self.email = email.downcase}

    devise :database_authenticatable, :registerable,
        :recoverable, :rememberable, :trackable, :validatable

    validates :name, presence: true, length: {maximum: 50}
    validates :email, presence: true, :email => {:ban_disposable_email => true, :message => I18n.t('validations.errors.models.user.invalid_email')}, uniqueness: { case_sensitive: false }

    validates :password, presence: true, length: {minimum: 6},:if=>:password_validation_required?

    LOGO_TYPES = ['image/jpeg', 'image/png', 'image/gif']
    has_attached_file :avatar, :styles => {:medium => "300x300>",:square=>"200x200>", :thumb => "100x100>" }, :default_url => '/assets/missing_:style.png'
    validates_attachment_content_type :avatar, :content_type => LOGO_TYPES

    def password_validation_required?
        !@password.blank?
    end
end
Run Code Online (Sandbox Code Playgroud)

客户:

class Client < User
    validates :industry, presence: true
    validates :city, presence: true
    validates :state, presence: true
    validates :description, presence: true, length: {minimum: 50, maximum: 300}
end
Run Code Online (Sandbox Code Playgroud)

提供者:

class Provider < User
    validates :ptype, presence: true
    validates :city, presence: true
    validates :state, presence: true
    validates :education, presence: true
    validates :biography, presence:true, length: {minimum: 50, maximum: 300}
    validates_format_of :linkedin, :with => URI::regexp(%w(http https))
    validates :resume, presence: true
    has_many :disciplines 
end
Run Code Online (Sandbox Code Playgroud)

这是我的控制器:

class SessionsController < Devise::SessionsController
    def create
        rtn = super
        sign_in(resource.type.underscore, resource.type.constantize.send(:find,resource.id)) unless resource.type.nil?
        rtn
    end
end

class RegistrationsController < Devise::RegistrationsController
    protected
    def after_sign_up_path_for(resource)
        if resource.is_a?(User)
            if current_user.is_a?(Client)
                edit_client_path(current_user.id)
            elsif current_user.is_a?(Provider)
                edit_provider_path(current_user.id)
            end
        else
            super
        end
    end
end

class ClientsController < ApplicationController
    def show
        @client = Client.find(params[:id])
    end
    def edit
        @client = Client.find(params[:id])
    end
    def update
        @client = Client.find(params[:id])
        if @client.update_attributes(client_params_edit)
            flash[:success] = "Profile Updated"
            redirect_to @client
        else
            flash[:failure] = "Profile Information Invalid"
            render 'edit'
        end
    end
    def client_params_edit
        params.require(:client).permit(:avatar,:industry,:city,:website, :description)
    end
end
Run Code Online (Sandbox Code Playgroud)

提供者控制器非常相似.

最后,这是我的schema.rb:

ActiveRecord::Schema.define(version: 20140628213816) do

  create_table "disciplines", force: true do |t|
    t.integer "years"
    t.string  "description"
    t.integer "user_id"
  end

  create_table "users", force: true do |t|
    t.string   "name"
    t.string   "email"
    t.string   "avatar_file_name"
    t.string   "avatar_content_type"
    t.integer  "avatar_file_size"
    t.datetime "avatar_updated_at"
    t.string   "password_digest"
    t.string   "industry"
    t.string   "city"
    t.string   "state"
    t.string   "website"
    t.string   "description"
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "type"
    t.string   "ptype"
    t.string   "education"
    t.string   "resume_file_name"
    t.string   "resume_content_type"
    t.integer  "resume_file_size"
    t.datetime "resume_updated_at"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

end
Run Code Online (Sandbox Code Playgroud)

Car*_*ela 0

只是一个想法。您是否考虑过允许注册为用户并将类型参数保留到工作流程的后期。

IE

注册页面:创建用户(使用一个参数决定用户最终将成为哪种类型)

第二页(您在创建用户时自动重定向到该页面,甚至以未完成第 2 部分的用户身份登录):添加适当的必需信息,并在提交时将类型从“用户”更改为适当的 STI 类型。

另一种选择是将您的第一个“提交”按钮替换为仅通过 JS 显示相关额外字段(以及真正的提交按钮)的按钮。