Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i include Rails join table field in the form?

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.

View/form:

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

Models

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
like image 996
katie Avatar asked Apr 26 '12 02:04

katie


1 Answers

<%= f.fields_for :memberships do |m| %>
  <%= m.text_field :task %>
  <%= m.fields_for :clubs do |s| %>
    <%= s.text_field :name %>
  <% end %>
<% end %>
like image 91
DGM Avatar answered Sep 23 '22 17:09

DGM