Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 为什么rails没有';t在单表继承中保存类型字段_Ruby On Rails_Inheritance - Fatal编程技术网

Ruby on rails 为什么rails没有';t在单表继承中保存类型字段

Ruby on rails 为什么rails没有';t在单表继承中保存类型字段,ruby-on-rails,inheritance,Ruby On Rails,Inheritance,我有一个模型客户端,具有单表继承。 但是当我尝试提交表单时,类型字段没有保存在数据库中。如何强制它保存类型,然后在index.html.erb上显示帐户类型 型号/客户端.rb class Client < ActiveRecord::Base end class Suscriber < Client end class NonSuscriber < Client end <%= simple_form_for @client do |f| %>

我有一个模型客户端,具有单表继承。 但是当我尝试提交表单时,类型字段没有保存在数据库中。如何强制它保存类型,然后在index.html.erb上显示帐户类型

型号/客户端.rb

class Client < ActiveRecord::Base

end

class Suscriber < Client

end

class NonSuscriber < Client

end 
    <%= simple_form_for @client do |f| %>

      <%= f.input :name %>
      <%=f.input :type %>

      <%= f.button :submit %>   


<% end %>
def index
  @clients = Client.where(:type => params[:type])
    respond_to do |format|
      format.html
      format.json {render json: @clients}
    end
end 


 def new
   @client = Client.new 

      respond_to do |format|
      format.html # new.html.erb
      format.json { render :json => @client }
    end
end

def create
    @client = Client.new(params[:client])

    respond_to do |format|
      if @client.save
        format.html { redirect_to @clinet, :notice => 'Client was successfully created.' }
        format.json { render :json => @client, :status => :created, :location => @client }
      else
        format.html { render :action => "new" }
        format.json { render :json => @client.errors, :status => :unprocessable_entity }
      end
    end
  end      
我在rails 3.1上

说:

Active Record允许通过将类的名称存储在默认名为“type”的列中(可以通过覆盖Base.heritation\u列来更改)来进行继承

如文档中所述,您需要使用
设置继承列
,查看

class客户端

HTH

您的控制器在做什么?我添加了控制器索引、创建和新建
class Client < ActiveRecord::Base
  set_inheritance_column do
    original_inheritance_column + "_id" # replace original_inheritance_column with "type" 
  end
end