tvinod@gmail.com
2009-May-11 15:47 UTC
updating join table (HMT) with checkbox and extra fields
Hi,
I''ve been breaking my head over this thing with no success. Here is my
table layout -
class User < AR
has_many :memberships
has_many :groups, :through => :memberships
...
class Group < AR
has_many :memberships
has_many :users, :through => :memberships
...
class Membership < AR
belongs_to :user, :class_name => "User", :foreign_key =>
"user_id"
belongs_to :group, :class_name => "Group", :foreign_key =>
"Group_id"
...
Now, Membership table has an extra field called membership_id and I
want the user to fill in that id in the edit form.
My view looks like this. The checkbox fields works great and I can
update my db pretty well (barring minor issues).
<% form_for :user, @user, :url => { :action =>
''save_memberships''} do |
form| %>
<%= form.error_messages %>
<% @groups.each do |g| %>
<%= check_box_tag("user[membership_ids][]", g.id,
@user.groups.include?(g)) %>
<%= "#{g.name}"%><br>
----> I have to invoke my partial to collect membership id here. But
can''t get the call right. The partial is below.
<% end%>
<% end%>
This is the membership partial -
<div class="membership">
<% fields_for "user[membership_ids][]", membership do |f| %>
Membership id: <%= f.text_field :membership_id %>
<% end %>
</div>
Any ideas how I can do this?
thanks
Trek Glowacki
2009-May-11 15:56 UTC
Re: updating join table (HMT) with checkbox and extra fields
Hi, rubyonrails-core is for discussing the development _of_ Rails. Repost this on rubyonrails-talk@googlegroups.com for help with development _using_ Rails. Additionally, you may want to see http://wonderfullyflawed.com/2009/02/17/rails-forms-microformat/ for a detailed explanation of what HTML form helpers output and how to use this format for custom behaviors. -Trek On Mon, May 11, 2009 at 11:47 AM, tvinod@gmail.com <tvinod@gmail.com> wrote:> > Hi, > I''ve been breaking my head over this thing with no success. Here is my > table layout - > > class User < AR > has_many :memberships > has_many :groups, :through => :memberships > ... > > class Group < AR > has_many :memberships > has_many :users, :through => :memberships > ... > > class Membership < AR > belongs_to :user, :class_name => "User", :foreign_key => "user_id" > belongs_to :group, :class_name => "Group", :foreign_key => > "Group_id" > ... > > Now, Membership table has an extra field called membership_id and I > want the user to fill in that id in the edit form. > > My view looks like this. The checkbox fields works great and I can > update my db pretty well (barring minor issues). > > <% form_for :user, @user, :url => { :action => ''save_memberships''} do | > form| %> > <%= form.error_messages %> > <% @groups.each do |g| %> > <%= check_box_tag("user[membership_ids][]", g.id, > @user.groups.include?(g)) %> > <%= "#{g.name}"%><br> > ----> I have to invoke my partial to collect membership id here. But > can''t get the call right. The partial is below. > <% end%> > <% end%> > > This is the membership partial - > <div class="membership"> > <% fields_for "user[membership_ids][]", membership do |f| %> > Membership id: <%= f.text_field :membership_id %> > <% end %> > </div> > > Any ideas how I can do this? > > thanks > > > > >