如何让 fields_for 同时创建新对象和多对多关系?
我一直在跟踪这些 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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您偶然发现了一个相当过时的 Railscast。有些年龄很大……这个则不那么老了。您正在寻找的是 Rails 2.3 中引入的 accepts_nested_attributes_for 。
组模型:
组控制器:
新组操作:
用户部分:
作为一般经验法则,您应该为表单块变量使用更具描述性的名称,尤其是对于嵌套表单。
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:
Group Controller:
New Group action:
User partial:
As a general rule of thumb, you should use more descriptive names for your form block variables, especially with nested forms.