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

发布于 2024-12-02 09:27:27 字数 3414 浏览 8 评论 0原文

我一直在跟踪这些 Railscast 并尝试修改代码,以便它可以与 Rails 3 一起使用:

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

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

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

我的表单如下所示:

表单创建者

创建者:

views/groups/new.html.rbviews

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

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

提前非常非常感谢:)

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:

The form creator

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 => '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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事和酒 2024-12-09 09:27:27

您偶然发现了一个相当过时的 Railscast。有些年龄很大……这个则不那么老了。您正在寻找的是 Rails 2.3 中引入的 accepts_nested_attributes_for

组模型:

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文