Ruby on rails 为什么rails没有';t在单表继承中保存类型字段
我有一个模型客户端,具有单表继承。 但是当我尝试提交表单时,类型字段没有保存在数据库中。如何强制它保存类型,然后在index.html.erb上显示帐户类型 型号/客户端.rbRuby 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| %>
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