5

在这种情况下,我可以将值插入到 Member 表和 Club 表中。但是在我想提交一个值:task的表中有一个名为的字段,并且在 Memberships 表中并由Rails 自动插入。如何在下面的表格中包含该字段?先感谢您。membershipsmember_idclub_idtask

查看/表格:

<%= 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
4

1 回答 1

5
<%= f.fields_for :memberships do |m| %>
  <%= m.text_field :task %>
  <%= m.fields_for :clubs do |s| %>
    <%= s.text_field :name %>
  <% end %>
<% end %>
于 2012-04-26T04:25:50.910 回答