在嵌套的rails表单上创建多对多关系

时间:2011-09-01 06:59:04

标签: ruby-on-rails ruby ruby-on-rails-3 forms many-to-many

我正在尝试同时创建组,用户和成员资格(多对多关系)。人们可以在创建组时将用户添加到组中,然后我希望它通过所有成员路由到组的视图。我可以创建用户和当前用户的成员身份以保存到数据库。但是我很难获得新创建的User对象的id,以便保存新的成员资格。

Group.rb

class Group < ActiveRecord::Base
  has_many :memberships
  has_many :users, :through => :memberships
  has_many :users

  accepts_nested_attributes_for :users, :reject_if => lambda { |a| a[:name].blank? or a[:number].blank? }
end

Membership.rb

class Membership < ActiveRecord::Base
  belongs_to :groups
  belongs_to :users
end

User.rb

class User < ActiveRecord::Base
  has_many :memberships
  has_many :groups, :through => :memberships
  belongs_to :group
end

groups_controller.rb

class GroupsController < ApplicationController

  def show
    @group = Group.find(params[:id])    
    respond_to do |format|
      format.html
    end
  end

  def new
    @group = Group.new

    3.times { @group.users.build }
  end

  def create 
    @group = Group.new(params[:group]) 
    @group.memberships.build(:user_id => current_user.id)
    logger.info @group.users

    @group.users.each do |x|
      @group.memberships.build(:user_id => x.id )
    end

    respond_to do |format|
      if @group.save
        format.html { redirect_to(@group, :notice => 'Success!') }
      else
        format.html { render :action => "new" }
      end
    end

  end
end

我的表单如下:

The form creator

由以下人员创建:

的观点/组/ new.html.rb

<h1>New group</h1>

<%= form_for(@group) do |form| %>
<fieldset>
  <legend>Create a new group</legend>
  <%= render 'shared/group_error_messages', :object => form.object %>  
  <div class="clearfix">
    <%= form.label :group_name %>
    <div class="input">
      <%= form.text_field :group_name %>
    </div>
  </div>

  <div id="users">
    <div class="user">
      <%= form.fields_for :users, :url => {:action => 'new', :controller => 'users', :registered => false}, do |user_form| %>
      <%= render 'user', :user => user_form %>
      <% end %>
    </div>
  </div>
</div>

<div class="actions">
  <%= form.submit :value => 'Create your group', :class => 'btn success'%>
</div>
</fieldset>
<% end %>

的观点/组/ _user.html.rb

<div class="clearfix">
  <%= user.label :name %>
  <div class="input">
    <%= user.text_field :name %>
  </div>
</div>
<div class="clearfix">
  <%= user.label :number %>  
  <div class="input">
    <%= user.text_field :number %>
  </div>

</div>
<div class="clearfix" style="display:none">
  <%= user.label :password %>  
  <div class="input">
    <%= user.password_field :password, :value => newpass(6) %><!-- todo: fix this horrible hack -->
  </div>
</div>
<div class="clearfix" style="display:none">
  <%= user.label :registered %>  
  <div class="input">
    <%= user.text_field :registered, :value => false %> <!-- todo: fix this horrible hack -->
  </div>
</div>
<div class="clearfix" style="display:">
  <%= user.label :id %>  
  <div class="input">
    <%= user.text_field :id %> <!-- todo: fix this horrible hack -->
  </div>
</div>

因此,我的主要愿望是在创建用户ID时使用这些代码来查找用户ID,然后使用这些ID在组和用户之间构建成员资格。

非常非常非常感谢<)>

1 个答案:

答案 0 :(得分:5)

我认为

has_many :users, :through => :memberships
has_many :users

错误,您不能拥有两个或多个具有相同名称的关联。

尝试

has_many :users, :through => :memberships
has_many :members, :class_name => 'User', :foreign_key => :user_id

并在视图和控制器中进行相应的更改。像

form.fields_for : members