在这种情况下,我可以将值插入Member表和Club表。但是我想向其提交值的:task
表中有一个名为memberships
的字段,而在Memberships表中,Rails会自动插入member_id
和club_id
。如何在下面的表格中添加task
字段?先感谢您。
查看/表格:
<%= 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
最佳答案
<%= f.fields_for :memberships do |m| %>
<%= m.text_field :task %>
<%= m.fields_for :clubs do |s| %>
<%= s.text_field :name %>
<% end %>
<% end %>
关于ruby-on-rails - 如何在表单中包括Rails联接表字段?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10326551/