在嵌套的Rails表单上创建多对多关系 [英] Creating many-to-many relationships on a nested rails form

查看:100
本文介绍了在嵌套的Rails表单上创建多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试同时创建组,用户和成员资格(多对多关系).人们可以在创建组时将用户添加到组中,然后我希望它可以路由到具有所有成员的组视图.我可以获取要创建的用户,并将当前用户的成员资格保存到数据库中.但是,我正在努力获取新创建的User对象的ID,以保存新的成员资格.

I am trying to create Groups, Users and Memberships (the many-to-many relationships) simultaneously. People can add users to the group as they create it and then I want it to route through to a view of the group with all the members. I can get the users to be created and the current user's membership to save to the database. However I'm struggling to get the id for the freshly created User objects in order to save the new memberships.

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

我的表单如下:

哪个创建者:

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

views/groups/_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在组和用户之间建立成员资格.

So my main desire is to get this bit of code to find the User ids as they are created and then use those ids to build the memberships between the groups and users.

非常非常感谢:)

Thanks very, very much in advance :)

推荐答案

我认为

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

是错误的,不能有两个或两个以上具有相同名称的关联.

is wrong, you can't have two or more associations with same name .

尝试做

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

并在视图和控制器中进行相应的更改.喜欢

and make corresponding changes in views and controllers . Like

form.fields_for : members

这篇关于在嵌套的Rails表单上创建多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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