如何让fields_for同时创建新对象和多对多关系?

时间:2011-08-31 09:18:20

标签: ruby-on-rails ruby-on-rails-3 many-to-many fields-for

我一直在关注这些Railscast并尝试修改代码,以便它适用于Rails 3:

http://railscasts.com/episodes/73-complex-forms-part-1

http://railscasts.com/episodes/73-complex-forms-part-2

http://railscasts.com/episodes/73-complex-forms-part-3

我正在尝试同时创建组,用户和成员资格(多对多关系)。人们可以在创建组时将用户添加到组中,然后我希望它通过所有成员路由到组的视图。我可以获得会员资格,但创建用户并将其与群组关联起来却很困难。我的代码目前看起来像这样:

Group.rb

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

  attr_accessible :group_name

  def user_attributes=(user_attributes)
    user_attributes.each do |attributes|
      users.build(attributes)
    end
  end
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
end

groups_controller.rb

class GroupsController < ApplicationController
  def create #todo test for number of groups they're already in before creation
    @group = Group.new(params[:group])
    @group.memberships.build(:user_id => current_user.id)
    #@group..build

    respond_to do |format|
      if @group.save
        format.html { redirect_to(@group, :notice => 'Group was successfully created and user added...?') }
      else
        format.html { render :action => "new" }
      end
    end
  end
end

我的表单如下:

The form creator

由以下人员创建:

的观点/组/ new.html.rb

<h1>New group</h1>

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

  <div id="users">
    <%= render :partial => 'user', :collection => @group.users %>
  </div>

</div>
<p><%= add_user_link "Add a member" %></p>


<div class="actions">
  <%= f.submit :value => 'Create your group', :class => 'btn success'%>

  <!-- todo test for if they already have a group...-->
</div>
</fieldset>
<% end %>

的观点/组/ _user.html.rb

<div class="user">
  <%= fields_for :user do |f| %>
    <div class="clearfix">
    <%= f.label :name %>
    <div class="input">
      <%= f.text_field :name %>
    </div>
  </div>
  <div class="clearfix">
    <%= f.label :number %>  
    <div class="input">
      <%= f.text_field :number %>
    </div>
  </div>
  <%= link_to_function "remove", "$(this).up('.user').remove()" %>
  <% end %>
</div>

非常非常非常感谢<)>

1 个答案:

答案 0 :(得分:2)

你偶然发现了一个相当过时的Railscast。有些年龄很好......这个没那么多。您正在寻找的是accepts_nested_attributes_for,它是在rails 2.3中引入的。

小组模特:

#app/models/group.rb
class Group < ActiveRecord::Base
  has_many :memberships
  has_many :users, :through => :memberships

  accepts_nested_atrributes_for :users
end

组控制器:

#app/controllers/groups_controller.rb
Class GroupsController < ApplicationController
  def create
    @group = Group.new(params[:group)
    @group.save ? redirect_to(@group) : render("new")
  end
end

新群组行动:

  <!-- Giving you the important changes only -->
  <div id="users">
    <%= render :partial => 'user', :collection => @group.users, :locals => { :form => f } %>
  </div>

用户偏:

<div class="user">
  <%= form.fields_for :user do |f| %>

作为一般经验法则,您应该为表单块变量使用更多描述性名称,尤其是嵌套表单。