我正在学习 RoR 并尝试使用 Accepts_nested_attributes_for 和 has_and_belongs_to_many 来提交传统上是两种形式的信息。我在一些网站上读过它们兼容,一些网站不兼容,还有一些网站不知道。作为参考,我使用的是 Rails 2.3.4。我尝试从 Ryan 的 Scraps嵌套模型教程中对我的解决方案进行建模
从我尝试调试的内容来看,我似乎有两个问题,但我不知道为什么。
- 当我提交带有嵌套模型的表单时,只发布了部分嵌套模型信息。我只得到第一个字段,而不是用户可能选择的其他“n”个字段
- 在发布的单个字段中,没有任何行插入到我为 HABTM 关系创建的连接表中。
这是我的插入尝试的一段代码和相应的日志:
律师模特:
class Attorney < ActiveRecord::Base
has_and_belongs_to_many :associations
accepts_nested_attributes_for :associations, :reject_if => proc { |a| a['name'].blank? }
end
关联模型:
class Association < ActiveRecord::Base
has_and_belongs_to_many :attorneys
accepts_nested_attributes_for :attorneys
validates_presence_of :name, :message => "Please enter an association name."
end
律师总监:
def new
@attorney = Attorney.new
@attorney.associations.build
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @attorney }
end
end
def create
@attorney = Attorney.new(params[:attorney])
respond_to do |format|
if @attorney.save
flash[:notice] = 'Attorney was successfully created.'
format.html { redirect_to(@attorney) }
format.xml { render :xml => @attorney, :status => :created, :location => @attorney }
else
format.html { render :action => "new" }
format.xml { render :xml => @attorney.errors, :status => :unprocessable_entity }
end
end
end
律师新观点:
<% form_for(@attorney, :html => {:multipart => true}) do |f| %>
<%= f.error_messages %>
<%= f.label :"First name" %>
<%= f.text_field :firstname %><br>
<%= f.label :"Last Name" %>
<%= f.text_field :lastname %><br>
<%= f.label :"Attorney Type" %>
<%= f.collection_select :member_type_id, MemberType.all, :id, :name %><br>
<%= f.text_area :bio, :cols => 70, :rows => 20 %><br><br>
<%= f.label :"Attorney Location" %>
<%= f.collection_select :office_location_id, OfficeLocation.all, :id, :location %><br>
<div id="associations">
<%= render :partial => 'shared/membership' %>
</div>
<%= add_association_link "Add Association" %>
<%= f.submit 'Create' %>
<% end %>
部分会员:
<div class="association">
<% fields_for :associations do |assoc_form| %>
<%= assoc_form.collection_select(:association_id, Association.find(:all), :id, :name, :include_blank => true) %>
<%= link_to_function "remove", "$(this).up('.association').remove()" %> <%= link_to '新关联', new_association_path %> <% end %>
律师助理链接:
def add_association_link(name)
link_to_function name do |page|
page.insert_html :bottom, :associations, :partial => 'shared/membership', :object => AssociationsAttorneys.new
end
end
加入表迁移:
class CreateAssociationsAttorneys < ActiveRecord::Migration
def self.up
create_table :associations_attorneys do |t|
t.references :attorney, :null => false
t.references :association, :null => false
t.timestamps
end
end
def self.down
drop_table :associations_attorneys
end
end
日志捕获:
Processing AttorneysController#new (for 127.0.0.1 at 2009-12-04 08:16:19) [GET]
Rendering template within layouts/default
Rendering attorneys/new
[4;35;1mMemberType Load (0.4ms)[0m [0mSELECT * FROM "member_types" [0m
[4;36;1mOfficeLocation Load (18.6ms)[0m [0;1mSELECT * FROM "office_locations" [0m
[4;35;1mAssociation Load (0.6ms)[0m [0mSELECT * FROM "associations" [0m
Rendered shared/_membership (3.5ms)
[4;36;1mCACHE (0.0ms)[0m [0;1mSELECT * FROM "associations" [0m
Rendered shared/_membership (1.5ms)
Rendered shared/_nav (0.6ms)
Rendered shared/_footer (0.1ms)
Completed in 149ms (View: 114, DB: 20) | 200 OK [http://localhost/attorneys/new]
Processing ApplicationController#index (for 127.0.0.1 at 2009-12-04 08:16:19) [GET]
Processing AttorneysController#create (for 127.0.0.1 at 2009-12-04 08:16:57) [POST]
Parameters: {"commit"=>"Create", "authenticity_token"=>"Jh7aMCcOY7jUu/D1YtiCswg2n6iwqnS98VnVn46psp0=", "associations"=>{"association_id"=>"3"}, "attorney"=>{"birthstate"=>"Alabama", "office_location_id"=>"1", "birthdate"=>"December 3, 2009", "birthcity"=>"Test", "middlename"=>"Test", "lastname"=>"Testing", "image_temp"=>"", "member_type_id"=>"2", "firstname"=>"Test", "bio"=>"testing testing testing", "suffix"=>"", "email"=>"testing@test.com"}}
[4;35;1mAttorney Load (15.6ms)[0m [0mSELECT "attorneys".id FROM "attorneys" WHERE ("attorneys"."email" = 'testing@test.com') LIMIT 1[0m
[4;36;1mAttorney Create (0.8ms)[0m [0;1mINSERT INTO "attorneys" ("birthstate", "created_at", "birthdate", "office_location_id", "birthcity", "updated_at", "middlename", "lastname", "firstname", "member_type_id", "suffix", "bio", "image", "email") VALUES('Alabama', '2009-12-04 15:16:57', 'December 3, 2009', 1, 'Test', '2009-12-04 15:16:57', 'Test', 'Testing', 'Test', 2, '', 'testing testing testing', NULL, 'testing@test.com')[0m
Redirected to http://localhost:3000/attorneys/11
Completed in 150ms (DB: 16) | 302 Found [http://localhost/attorneys]
我可以看到 associations"=>{"association_id"=>"3"} 它只获得了我为特定人拥有的多个关联中的最后一个,并且没有在连接表中创建任何条目。在哪里可能我的代码出错了?