1

我有一个可以有很多事务的用户模型。事务可以是 type1、type2 或 type3 中的一种,每种类型都有不同的属性集。例如:type1 包含属性 a、b 和 c;而 type2 包含属性 a、c、d 和 e。User、Transaction 和 Type 模型定义如下:

class User < AR::Base
  has_many :transactions
end

class Transaction < AR::Base
  belongs_to :user
  belongs_to :transactionable, polymorphic: true
end

class Type1 < AR::Base
  has_many :transactions, as: :transactionable
end

class Type2 < AR::Base
  has_many :transactions, as: :transactionable
end

class Type3 < AR::Base
  has_many :transactions, as: :transactionable
end

现在,在生成 html 表单时,我有

/app/views/user/new.html.erb

<%= form_for @user do |f| %>
  ***
<% end %>

/app/views/user/show.html.erb

<%= form_for ... do |f| %>
  <%= f.text_field: a %>
  <%= f.text_field: b %>
  <%= f.text_field: c %>
<% end %>

在后面的form_for helper中,我想收集type1模型属性的用户输入数据。任何人都可以帮助在后面的 form_for 助手中应该输入什么内容吗?我试过@user.transactions.build_type1,但它显示错误。知道可能出了什么问题吗?

4

0 回答 0