Rails - 如何从单表继承创建正确类型的对象?

时间:2015-01-12 22:08:47

标签: ruby-on-rails single-table-inheritance

我已经设置了一个Rails项目来使用单表继承,因为我有两种类型的User s - SenderReceiver s。 Sender具有public_key属性,Receiver具有phone_number属性。他们通过name分享emailpasswordUser属性。

我的问题是,在create控制器的User功能中,我正在尝试创建一种或另一种类型 - SenderReceiver - 根据我的注册表单上的单选按钮的值。

以下是设置:

用户模型

class User < ActiveRecord::Base

  validates :name,  :presence => true, :length => { maximum: 50 }
  validates :password, presence: true

  self.inheritance_column = :user_type

  # We will need a way to know which types with subclass the User model
  def self.user_types
    %w(Sender Receiver)
  end
end

class Sender < User; end
class Receiver < User; end

SENDER MODEL

class Sender < User
  validates :public_key,  :presence => true
end

接收器模型

class Receiver < User
  validates :phone_number,  :presence => true, :length => 10
end

用户控制器

class UsersController < ApplicationController
  #  before_action :set_user, only: [:show, :edit, :update, :destroy]
  before_action :set_user_type

  def index
    @users = user_type_class.all
  end

  def show
    @user = User.find(params[:id])
  end

  def new
    @user = User.new
  end

  def create
    if(user_type.eql? "receiver")
      @user = Receiver.new(user_params)
    else
      @user = Sender.new(user_params)
  end
  ...
  private

    # allow views to access user_type
    def set_user_type
      @user_type = user_type
    end

    def user_type
      User.user_types.include?(params[:type]) ? params[:type] : "User"
    end

    def user_type_class
      user_type.constantize
    end

    # Use callbacks to share common setup or constraints between actions.
    def set_user
      @user = User.find(params[:id])
    end

    def user_params
      params.require(:user).permit(:name, :email, :password, :confirmation_password, :user_type)
    end
end

新用户表单

<%= form_for(@user) do |f| %>
...
  <div class="user-type">
    <%= f.label :user_type, class: 'form-control' %>
    <%= radio_button_tag(:user_type, "sender")  %>
    <%= label_tag(:user_type_sender, "I am a Sender")  %>
    <%= radio_button_tag(:user_type, "receiver")  %>
    <%= label_tag(:user_type_receiver, "I am a Receiver")  %>
  </div>
  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
 ...
<% end %>

create控制器的User方法中,我正在尝试使用此User语句生成正确类型的if

if(user_type.eql? "receiver")
  @user = Receiver.new(user_params)
else
  @user = Sender.new(user_params)

基于此处User形式记录的值:

<%= radio_button_tag(:user_type, "sender")  %>
<%= label_tag(:user_type_sender, "I am a Sender")  %>
<%= radio_button_tag(:user_type, "receiver")  %>
<%= label_tag(:user_type_receiver, "I am a Receiver")  %>

但是,总是最终使用Sender语句中的else类型对象。我认为这意味着我的if语句if(user_type.eql? "receiver")出现问题;但是,我无法弄清楚是什么。

思想?

1 个答案:

答案 0 :(得分:1)

首先,我认为您不需要set_user_type方法,因为您没有使用其设置的@user_type变量。从控制器顶部取出before_action

其次,在user_type方法中,您需要将params[:type]更改为params[:user_type],因为这是HTML中单选按钮标记的名称。

def user_type
  User.user_types.include?(params[:user_type]) ? params[:user_type] : "User"
end

第三,您还需要将单选按钮标签的值属性大写为&#34; Sender&#34;和&#34; Receiver&#34;,因为那就是User.user_types数组中的内容。

<%= radio_button_tag(:user_type, "Sender")  %>
<%= radio_button_tag(:user_type, "Receiver")  %>

免责声明:未经过测试,但它应该解决您的问题。