fields_for 与 through 关系

发布于 2024-11-25 01:02:55 字数 1851 浏览 2 评论 0原文

Item 从他的集合中获取 collection_fields。 对于集合项的每个 collection_field 可能有一个 field_value

models

class Item < ActiveRecord::Base
    belongs_to :collection
    has_many :field_values, :dependent => :destroy
    has_many :collection_fields, :through => :collection
    accepts_nested_attributes_for :field_values, :allow_destroy => true
end

class Collection < ActiveRecord::Base 
    has_many :items, :dependent => :destroy
    has_many :collection_fields, :dependent => :destroy
end

class CollectionField < ActiveRecord::Base
    belongs_to :collection
    belongs_to :field
    has_many :items, :through => :collection
    has_many :field_values, :dependent => :destroy
end

class Field < ActiveRecord::Base
    has_many :collection_fields  
end

class FieldValue < ActiveRecord::Base
    belongs_to :item
    belongs_to :collection_field
end

controller

def new
    @item = Item.new
    @item.collection = Collection.find(params[:collection])
    @item.collection.collection_fields.each do |cf|
        @item.collection_fields << cf
    end

def edit  
    @item = Item.find(params[:id])

view

<%= form_for(@item, :html => { :multipart => true }) do |f| %>
    <% @item.collection_fields.each do |cf| %>
        <% f.label cf.field.name %>
        <%= f.fields_for :field_values, cf.field_values.find_or_create_by_item_id(@item.id) do |fv| %>
            <%= fv.text_field :valore %>

此代码在编辑方法中工作正常,但是当我尝试添加一个新项目,我得到:

Couldn't find FieldValue with ID=213 for Item with ID=

如何正确实现这些表单字段?

Item gets the collection_fields from his collections.
For each collection_field of the collection item may have a field_value

models

class Item < ActiveRecord::Base
    belongs_to :collection
    has_many :field_values, :dependent => :destroy
    has_many :collection_fields, :through => :collection
    accepts_nested_attributes_for :field_values, :allow_destroy => true
end

class Collection < ActiveRecord::Base 
    has_many :items, :dependent => :destroy
    has_many :collection_fields, :dependent => :destroy
end

class CollectionField < ActiveRecord::Base
    belongs_to :collection
    belongs_to :field
    has_many :items, :through => :collection
    has_many :field_values, :dependent => :destroy
end

class Field < ActiveRecord::Base
    has_many :collection_fields  
end

class FieldValue < ActiveRecord::Base
    belongs_to :item
    belongs_to :collection_field
end

controller

def new
    @item = Item.new
    @item.collection = Collection.find(params[:collection])
    @item.collection.collection_fields.each do |cf|
        @item.collection_fields << cf
    end

def edit  
    @item = Item.find(params[:id])

view

<%= form_for(@item, :html => { :multipart => true }) do |f| %>
    <% @item.collection_fields.each do |cf| %>
        <% f.label cf.field.name %>
        <%= f.fields_for :field_values, cf.field_values.find_or_create_by_item_id(@item.id) do |fv| %>
            <%= fv.text_field :valore %>

This code is working fine with the edit method, but when I try to add a new item I get:

Couldn't find FieldValue with ID=213 for Item with ID=

How should I implement these form fields correctly?

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

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

发布评论

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

评论(1

你的呼吸 2024-12-02 01:02:55

我终于想出了一个解决方案。它不是那么优雅,但它有效

- @collection.collection_fields.each do |cf|
    = f.label cf.field.name
    - if @item.new_record?
        = f.fields_for :field_values,  @item.field_values.build() do |field_value|
            = field_value.text_field :valore
            = field_value.hidden_field :collection_field_id, :value => cf.id
    - else
        = f.fields_for :field_values, @item.field_values.find_or_create_by_collection_field_id(cf.id) do |field_value|
            %td= field_value.text_field :valore

I've finally worked out a solution. It's not so elegant, but it works

- @collection.collection_fields.each do |cf|
    = f.label cf.field.name
    - if @item.new_record?
        = f.fields_for :field_values,  @item.field_values.build() do |field_value|
            = field_value.text_field :valore
            = field_value.hidden_field :collection_field_id, :value => cf.id
    - else
        = f.fields_for :field_values, @item.field_values.find_or_create_by_collection_field_id(cf.id) do |field_value|
            %td= field_value.text_field :valore
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文