开发者

How to get fields_for to create new objects and many-to-many relationships simultaneously?

I've been following through these Railscasts and trying to amend the code so it works with 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

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 memberships to create just fine but having trouble creating users and associating them the group. My code currently looks like this:

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

My form looks like this:

How to get fields_for to create new objects and many-to-many relationships simultaneously?

Which is created by:

views/groups/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 =&g开发者_如何学Pythont; '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 %>

views/groups/_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>

Thanks very, very much in advance :)


You stumbled upon a fairly out of date Railscast it seems. Some age very well... this one not as much. What you're looking for is a accepts_nested_attributes_for which was introduced in rails 2.3.

Group model:

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

  accepts_nested_atrributes_for :users
end

Group Controller:

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

New Group action:

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

User partial:

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

As a general rule of thumb, you should use more descriptive names for your form block variables, especially with nested forms.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜