使用Formtastic分配嵌套属性

时间:2010-10-27 00:20:00

标签: ruby-on-rails ruby formtastic

我一直想把这个拿出来一段时间,但仍然没有运气。我有一个company_relationships表加入公司,存储一个额外的字段来描述名为'corp_credit_id'的关系的性质。我可以使表单正常工作以为Person添加company_relationships,但我似乎无法弄清楚如何设置该修饰符字段。有什么想法吗?

关于我的项目的更多信息:人们通过 company_relationships 拥有许多公司。有了这个额外的字段,我用它将所有特定的关系组合在一起。所以我可以将一个人的医生,承包商等分组。

我的模特:

Company.rb (删节)

class Company < ActiveRecord::Base
   include ApplicationHelper

has_many :company_relationships
has_many :people, :through => :company_relationships

Person.rb (删节)

class Person < ActiveRecord::Base
include ApplicationHelper

has_many :company_relationships
has_many :companies, :through => :company_relationships

accepts_nested_attributes_for :company_relationships

company_relationship.rb

class CompanyRelationship < ActiveRecord::Base
attr_accessible :company_id, :person_id, :corp_credits_id
belongs_to :company
belongs_to :person
belongs_to :corp_credits

end

我的形式部分,使用formtastic。

<% semantic_form_for @person do |f| %>
<%= f.error_messages %>
<% f.inputs do %>
 ...
<%= f.input :companies, :as => :check_boxes, :label => "Favorite Coffee Shops", :label_method => :name,  :collection => Company.find(:all, :conditions => {:coffee_shop => 't'}, :order => "name ASC"), :required => false %>

所以我想做的是:corp_credit_id =&gt;该输入中的“1”为咖啡店分配该属性。但是,formtastic似乎不允许这种分配发生。

关于如何做到这一点的任何想法?

1 个答案:

答案 0 :(得分:1)

你在寻找像

这样的东西吗?
  <% semantic_form_for @person do |form| %>
    <% form.semantic_fields_for :company_relationships do |cr_f| %>
    <%= cr_f.input :corp_credit_id  %>
<% end %>

位于documentation