我搜索并尝试了很多,但我不能完成它,因为我想..所以这里是我的问题。

class Moving < ActiveRecord::Base
  has_many :movingresources, :dependent => :destroy
  has_many :resources, :through => :movingresources
end

class Movingresource < ActiveRecord::Base
  belongs_to :moving
  belongs_to :resource
end

class Resource < ActiveRecord::Base
  has_many :movingresources
  has_many :movings, :through => :movingresources
end

Movingresources包含额外的字段,如quantity。我们正在努力为“法案”的意见。由于formtastic仅通过写简化整个关系的事情

<%= form.input :workers, :as => :check_boxes %>

和我得到一个真正的好复选框列表。但是,我还没有发现到目前为止是:我如何使用其他字段从“movingresource”,下一个或每个复选框从我的模型所需的字段

下?

我看到不同的方法,主要是通过对象的数组手动循环和创建适当的形式,使用:用于在form.inputs部,或没有。但是,没有这些解决方案的干净(例如工作了编辑视图而不是新的,因为所需的对象将不建立或产生的,并产生它们引起乱七八糟)。

我想知道你的方案来解决这个!

有帮助吗?

解决方案

好吧,我错过了accepts_nested_attributes_for革命,这就解释了为什么它不是真正的工作。

这让我一个很大的一步,但我想我的地方仍然有一些并发症与我的关系错综复杂^ _ ^

class Moving < ActiveRecord::Base
    has_many :movingworkers, :dependent => :destroy
    has_many :workers, :through => :movingworkers
    accepts_nested_attributes_for :movingworkers
end


<% form.inputs :for => :movingworkers do |movingworker| %>
    <%= movingworker.inputs :worker, :quantity %>
<% end %>

其他提示

Formtastic的:label_method选项可以提供帮助。 E.g。

<%= form.input :movingworkers, :label_method => :worker %>

<%= form.input :movingworkers, :label_method => Proc.new { |x| "#{x.worker} #{x.quantity}" } %>

如果该字段不在新视图存在,则可以只是测试,如果它是新的(的 new_record吗),如果您包装呈现出不同的字段集(入罐的局部是相当干净的方法)。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top