2015-02-24 160 views
0

我有三個型號:軌道4嵌套表單

class Item < ActiveRecord::Base 
    has_many :item_points 
    has_many :groups, through: :item_points 
    accepts_nested_attributes_for :item_points 
end 

class ItemPoint < ActiveRecord::Base 
    belongs_to :group 
    belongs_to :item 
    accepts_nested_attributes_for :group 
end 

class Group < ActiveRecord::Base 
    has_many :item_points 
    has_many :items, through: :item_points 
end 

items

create_table "items", force: :cascade do |t| 
    t.string "name",  null: false 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
end 

架構的架構item_points

create_table "item_points", force: :cascade do |t| 
    t.decimal "points",   null: false 
    t.integer "item_id",   null: false 
    t.integer "group_id", null: false 
    t.datetime "created_at",  null: false 
    t.datetime "updated_at",  null: false 
end 

的架構groups

create_table "groups", force: :cascade do |t| 
    t.string "name",  null: false 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
end 

在我的groups表中,我創建了許多行,例如, 組1組2

在表單創建items我很想看到每個字段爲group 1group 2,這樣我也許能爲該項目進入點。例如在項目X中,組1是10分,組2是5分。

編輯添加的形式

形式:

<%= form_for(@item) do |item_form| %> 

     <div class="form-group"> 
     <%= item_form.label :name %> 
     <%= item_form.text_field :name, :class => 'form-control' %> 
     </div> 

     <%= item_form.fields_for(:groups) do |groups_form| %> 
     <% group = groups_form.object_id.to_s%> 
     <%= groups_form.hidden_field :id %> 
     <%= groups_form.fields_for(:item_point) do |entity_form| %> 
      <%= entity_form.text_field :points %> 
     <% end %> 
     <% end %> 

這爲我提供了一個表格,其中包含一個額外的輸入框,稱爲item[groups][item_point][points],並且沒有標籤。

我想知道的是,我如何獲得全部我已經添加到組中的行作爲字段到Rails表單中?當我這樣做時,如何使用強參數保存關聯的item_points數據?

我花了相當一段時間尋找答案,我似乎無法找到除一系列StackOverflow問題之外的任何其他問題,這些問題似乎與我的問題看起來不太一樣。

所有的幫助是奇妙的讚賞。

+0

不是一個答案,但只是想指出,你需要在item_points表中將group_id更改爲item_group_id,或將'foreign_key:group_id'添加到'belongs_to:item_group' – Coenwulf 2015-02-24 15:57:22

+0

謝謝。這是我的錯字:) – glapworth 2015-02-24 16:05:43

回答

1

有一個有用的帖子,其中有一些例子:https://robots.thoughtbot.com/accepts-nested-attributes-for-with-has-many-through。它具體談到在您的關聯中添加inverse_of

在您看來,您需要使用fields_for。你可以(例如)把它放在一個表或一個列表中,並讓每個條目成爲一行。

如果您分享您迄今爲止在您的視圖中嘗試過的內容,則可以在需要時獲得更詳細的回覆。

至於在你的控制器permitted_params,你可以嵌套他們是這樣的:

def permitted_params 
    params.permit(item: [ 
    :name, 
    item_points_attributes: [ 
     :id, 
     :points, 
     :group_id, 
    ] 
) 
end 

更新:

不是fields_for(:groups)我想你想你的控制器建立所有item_points模型(@item_points = Group.all.collect {|group| ItemPoint.new({group_id: group.id, item_id: @item.id})。 然後您可以使用fields_for(:item_points, @item_points)

您可以爲該字段添加標籤,使其不僅僅是使用HTML標籤標籤的未標記字段。

+0

我已經添加了違規表格的一部分。感謝您一直以來的幫助。 – glapworth 2015-02-24 17:11:06

+1

而不是'fields_for(:groups)我想你想要你的控制器爲所有item_points建立模型('@item_points = Groups.collect {| group | ItemPoint.new({group_id:group.id,item_id:@item .id}')。然後你可以使用'fields_for(:item_points,@item_points)'。你可以爲該字段添加一個標籤,因此它不僅僅是一個使用HTML標籤標籤的未標記字段。 – Coenwulf 2015-02-24 19:37:40

+0

謝謝,不要把'collect'應用到ActiveRecord模型上,它需要被應用到範圍iirc – glapworth 2015-02-24 19:54:41