使用 Formtastic 分配嵌套属性

发布于 2024-09-29 05:04:48 字数 1615 浏览 0 评论 0原文

我已经尝试解决这个问题有一段时间了,但仍然没有运气。我有一个连接公司人员的company_relationships表,存储一个额外的字段来描述称为“corp_credit_id”的关系的性质。我可以让表单正常工作来为人员添加 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 =>该输入中的“1”为咖啡店分配该属性。但 formattastic 似乎不允许这种分配发生。

关于如何做到这一点有什么想法吗?

I've been trying to figure this one out for a while but still no luck. I have a company_relationships table that joins Companies and People, storing an extra field to describe the nature of the relationship called 'corp_credit_id'. I can get the forms working fine to add company_relationships for a Person, but I can't seem to figure out how to set that modifier field when doing so. Any ideas?

More about my project: People have many companies through company_relationships. With that extra field in there I am using it to group all of the specific relationships together. So I can group a person's Doctors, Contractors, etc.

My models:

Company.rb (abridged)

class Company < ActiveRecord::Base
   include ApplicationHelper

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

Person.rb (abridged)

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

My form partial, using 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 %>

So what I would like to do is something like :corp_credit_id => '1' in that input to assign that attribute for Coffee Shop. But formtastic doesn't appear to allow this assignment to happen.

Any ideas on how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心清如水 2024-10-06 05:04:49

您是否正在寻找类似

  <% semantic_form_for @person do |form| %>
    <% form.semantic_fields_for :company_relationships do |cr_f| %>
    <%= cr_f.input :corp_credit_id  %>
<% end %>

文档 中的内容

Are you looking for something like

  <% semantic_form_for @person do |form| %>
    <% form.semantic_fields_for :company_relationships do |cr_f| %>
    <%= cr_f.input :corp_credit_id  %>
<% end %>

It is in the documentation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文