2012-04-26 54 views
4

在這種情況下,我可以將值插入成員表和俱樂部表。但是在memberships表中有一個名爲:task的字段,我想向其提交一個值,並且在會員表中member_idclub_id由Rails自動插入。我如何在下面的表格中包含task字段?先謝謝你。我如何在表單中包含Rails連接表字段?

查看/形式:

<%= form_for @member ,:url=>{:action =>"create"} do |f| %> 
    <%= f.text_field :email %> 
    <%= f.fields_for :clubs do |s| %> 
    <%= s.text_field :name %> 
    <% end %> 
    <%= f.submit "submit" %> 
<% end %> 

模型

class Member < ActiveRecord::Base 
    has_many :clubs ,:through=> :memberships 
    has_many :memberships 
    accepts_nested_attributes_for :clubs 
    attr_accessible :clubs_attributes 
end 

class Club < ActiveRecord::Base 
    has_many :members ,:through=>:memberships 
    has_many :memberships 
end 

class Memberships < ActiveRecord::Base 
    belongs_to :Member 
    belongs_to :Club 
end 

回答

3
<%= f.fields_for :memberships do |m| %> 
    <%= m.text_field :task %> 
    <%= m.fields_for :clubs do |s| %> 
    <%= s.text_field :name %> 
    <% end %> 
<% end %> 
+0

謝謝@DGM你會在控制器中加入,使這個可能嗎? – BKSpurgeon 2016-06-10 03:18:21

相關問題