使用单表继承处理表单

时间:2011-03-28 06:48:50

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

我的个人资料编辑视图中有一个表单,从此行开始:

<% form_for @profile, :html => { :multipart => true } do |f| %>

配置文件经历单表继承,两个子类是Profile :: Artist和Profile :: Listener。

当我尝试访问配置文件的编辑视图时,出现此错误:

NoMethodError in Profiles#edit

Showing /rubyprograms/dreamstill/app/views/profiles/edit.html.erb where line #1 raised:

undefined method `profile_artist_path' for #<#<Class:0x103359a18>:0x1033560c0>

其中第1行是我在上面发布的表单的代码行。我该如何解决这个错误?

更新:

我将此代码添加到我的个人资料模型中:

def self.inherited(child)
  child.instance_eval do
    def model_name
      Vehicle.model_name
    end
  end
  super
end

现在我的错误已更改为:

NameError in Profiles#edit

Showing /rubyprograms/dreamstill/app/views/profiles/edit.html.erb where line #1 raised:

uninitialized constant Profile::Vehicle

更新2:

我将表单的第一行更改为:

<% form_for(:profile, @profile, :url => {:controller => "profiles", :action => "update"}, :html => { :multipart => true }) do |f| %>

<%= f.submit :profile %>的提交按钮

现在我刚收到路线错误......

1 个答案:

答案 0 :(得分:3)

不是Vehile而是Profile

def self.inherited(child)
  child.instance_eval do
    def model_name
      Profile.model_name
    end
  end
  super
end

def self.model_name
  name = "profile"
  name.instance_eval do
    def plural;   pluralize;   end
    def singular; singularize; end
    def i18n_key; singularize; end
    def human(*args); singularize; end
  end
  return name
end

<强>更新

实际问题出在形式上。您应该添加:method => :put

<%= form_for(@profile, :html => { :multipart => true, :method => :put }) do |f| %>