如何在表单中包括Rails联接表字段? [英] How do i include Rails join table field in the form?

查看:114
本文介绍了如何在表单中包括Rails联接表字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种情况下,我可以将值插入到Member表和Club表中.但是我要向其中提交值的memberships表中有一个名为:task的字段,而Memberships表中的member_idclub_id由Rails自动插入.如何在下面的表单中包含task字段?预先谢谢你.

In this scenario I can insert values to the Member table and the Club table. But there is a field called :task in the memberships table that I want to submit a value to, and in the Memberships table member_id and club_id are inserted automatically by Rails. How do I include the task field in the form below? Thank you in advance.

查看/表单:

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

这篇关于如何在表单中包括Rails联接表字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆