业余rails程序员难以使用fields_for表单构建器。我的rails应用程序(在v=2.3.11上)跟踪实验室协议和在这些协议上使用的动物(它们的权重等)。
我的具体问题是协议只能有一定数量的动物,但允许的数量是特定于一个物种的。
嵌套表单行为应该显示所有动物种类的列表和一个编辑框,其中包含协议允许的数量。
Models: Protocol, Allowed_Animal, Species
<<p> 物种模型/strong>class Species < ActiveRecord::Base
has_many :animals, :order => "band"
has_many :allowed_animals
end
<<p> 协议模型/strong> class Protocol < ActiveRecord::Base
has_and_belongs_to_many :animals, :order => "band"
has_many :allowed_animals
accepts_nested_attributes_for :allowed_animals
end
<<p> Allowed_Animal模型/strong> class AllowedAnimal < ActiveRecord::Base
belongs_to :protocol
has_one :species
end
allowed_animal的表包括:
id, protocol_id, species_id, number, created_at, updated_at
嵌套表单是这样工作的:
<% form_for(@protocol) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.label :number %><br />
<%= f.text_field :number %>
</p>
<p>
Number of animals allowed on protocol:
</p>
<div class="left_indent">
<table>
<% f.fields_for :allowed_animals, Species.all do |a| %>
<tr>
<td><i><%= a.object[:name] %></i></td>
<td><%= a.text_field :number, :value => @protocol.allowed_animals(a), :size=>2 %></td>
<%= a.text_field :species_id, :value => a.object[:id], :hidden => true %>
</tr>
<% end %>
</table>
</div>
<p>
Users on protocol:
</p>
<div class="left_indent">
<%= render :partial => 'protocols/users_on_protocol_form', :locals =>{:protocol => @protocol, :users => User.current} %>
</div>
<p>
<%= f.submit 'Save' %>
</p>
<% end %>
表单显示了我想要的样子,但是在提交之后,我得到了错误:
wrong number of arguments (0 for 1)
在控制器中,因为它试图做:
@protocol = Protocol.new(params[:protocol])
我不知道从这里该去哪里。我需要在我的模型中有一个构建方法吗?或者我需要在控制器中做一些特别的事情吗?我的form_builder代码是否有问题,以及它如何通过物种迭代?
任何想法?
应用程序跟踪:
/var/lib/gems/1.8/gems/activerecord-2.3.11/lib/active_record/nested_attributes.rb:343:in `allowed_animals'
/var/lib/gems/1.8/gems/activerecord-2.3.11/lib/active_record/nested_attributes.rb:343:in `send'
/var/lib/gems/1.8/gems/activerecord-2.3.11/lib/active_record/nested_attributes.rb:343:in `assign_nested_attributes_for_collection_association'
/var/lib/gems/1.8/gems/activerecord-2.3.11/lib/active_record/nested_attributes.rb:244:in `allowed_animals_attributes='
/var/lib/gems/1.8/gems/activerecord-2.3.11/lib/active_record/base.rb:2918:in `send'
/var/lib/gems/1.8/gems/activerecord-2.3.11/lib/active_record/base.rb:2918:in `assign_attributes'
/var/lib/gems/1.8/gems/activerecord-2.3.11/lib/active_record/base.rb:2914:in `each'
/var/lib/gems/1.8/gems/activerecord-2.3.11/lib/active_record/base.rb:2914:in `assign_attributes'
/var/lib/gems/1.8/gems/activerecord-2.3.11/lib/active_record/base.rb:2787:in `attributes='
/var/lib/gems/1.8/gems/activerecord-2.3.11/lib/active_record/base.rb:2477:in `initialize'
/media/Data/Animal-Record-Keeping/app/controllers/protocols_controller.rb:42:in `new'
/media/Data/Animal-Record-Keeping/app/controllers/protocols_controller.rb:42:in `create'
/var/lib/gems/1.8/gems/actionpack-2.3.11/lib/action_controller/base.rb:1333:in `send'
/var/lib/gems/1.8/gems/actionpack-2.3.11/lib/action_controller/base.rb:1333:in `perform_action_without_filters'
/var/lib/gems/1.8/gems/actionpack-2.3.11/lib/action_controller/filters.rb:617:in `call_filters'
/var/lib/gems/1.8/gems/actionpack-2.3.11/lib/action_controller/filters.rb:610:in `perform_action_without_benchmark'
/var/lib/gems/1.8/gems/actionpack-2.3.11/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
/var/lib/gems/1.8/gems/activesupport-2.3.11/lib/active_support/core_ext/benchmark.rb:17:in `ms'
/var/lib/gems/1.8/gems/activesupport-2.3.11/lib/active_support/core_ext/benchmark.rb:17:in `ms'
/var/lib/gems/1.8/gems/actionpack-2.3.11/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
/var/lib/gems/1.8/gems/actionpack-2.3.11/lib/action_controller/rescue.rb:160:in `perform_action_without_flash'
/var/lib/gems/1.8/gems/actionpack-2.3.11/lib/action_controller/flash.rb:151:in `perform_action'
/var/lib/gems/1.8/gems/actionpack-2.3.11/lib/action_controller/base.rb:532:in `send'
/var/lib/gems/1.8/gems/actionpack-2.3.11/lib/action_controller/base.rb:532:in `process_without_filters'
/var/lib/gems/1.8/gems/actionpack-2.3.11/lib/action_controller/filters.rb:606:in `process'
/var/lib/gems/1.8/gems/actionpack-2.3.11/lib/action_controller/base.rb:391:in `process'
/var/lib/gems/1.8/gems/actionpack-2.3.11/lib/action_controller/base.rb:386:in `call'
/var/lib/gems/1.8/gems/actionpack-2.3.11/lib/action_controller/routing/route_set.rb:438:in `call'
参数:
{"commit"=>"Save",
"protocol"=>{"number"=>"Test number",
"start_date(1i)"=>"2011",
"start_date(2i)"=>"4",
"title"=>"Test title",
"start_date(3i)"=>"12",
"flight_exempt"=>"0",
"allowed_animals_attributes"=>{"0"=>{"number"=>"7",
"id"=>"2",
"species_id"=>"2"},
"1"=>{"number"=>"11",
"id"=>"3",
"species_id"=>"3"},
"2"=>{"number"=>"13",
"id"=>"4",
"species_id"=>"4"},
"3"=>{"number"=>"17",
"id"=>"5",
"species_id"=>"5"}},
"summary"=>"Test summary"},
"user_id"=>{"33"=>"0",
"6"=>"0",
"14"=>"0",
"9"=>"0",
"37"=>"0",
"38"=>"0",
"1"=>"1",
"39"=>"0",
"40"=>"0",
"18"=>"0",
"31"=>"0",
"21"=>"0",
"32"=>"0",
"5"=>"0"}}
开发日志
# Logfile created on Tue Apr 12 18:11:11 -0400 2011DEPRECATION WARNING: Giving :session_key to SessionStore is deprecated, please use :key instead. (called from new at /var/lib/gems/1.8/gems/actionpack-2.3.11/lib/action_controller/middleware_stack.rb:72)
[4;36;1mSQL (0.1ms)[0m [0;1mSET SQL_AUTO_IS_NULL=0[0m
Processing MainController#index (for 127.0.0.1 at 2011-04-12 18:11:12) [GET]
::: Checking session expiry
::: Session has not expired. Reinitialising.
::: Initializing session expiry. Expires at Tue Apr 12 18:26:12 -0400 2011
[4;35;1mUser Load (0.1ms)[0m [0mSELECT * FROM `users` WHERE (end_date is null) ORDER BY name[0m
[4;36;1mUser Load (0.1ms)[0m [0;1mSELECT * FROM `users` WHERE (end_date is null and job_type regexp 'Weekend') ORDER BY name[0m
[4;35;1mUser Load (0.1ms)[0m [0mSELECT * FROM `users` WHERE (end_date is null and job_type regexp 'Medic') ORDER BY name[0m
[4;36;1mUser Load (0.2ms)[0m [0;1mSELECT * FROM `users` WHERE (end_date is null and job_type regexp 'Anim') ORDER BY name[0m
Rendering template within layouts/main
Rendering main/index
Rendered layouts/_standard_head (3.2ms)
[4;35;1mUser Columns (0.5ms)[0m [0mSHOW FIELDS FROM `users`[0m
[4;36;1mUser Load (0.1ms)[0m [0;1mSELECT * FROM `users` WHERE (`users`.`id` = 1) [0m
[4;35;1mCACHE (0.0ms)[0m [0mSELECT * FROM `users` WHERE (`users`.`id` = 1) [0m
[4;36;1mCACHE (0.0ms)[0m [0;1mSELECT * FROM `users` WHERE (`users`.`id` = 1) [0m
[4;35;1mCACHE (0.0ms)[0m [0mSELECT * FROM `users` WHERE (`users`.`id` = 1) [0m
[4;36;1mCACHE (0.0ms)[0m [0;1mSELECT * FROM `users` WHERE (`users`.`id` = 1) [0m
[4;35;1mCACHE (0.0ms)[0m [0mSELECT * FROM `users` WHERE (`users`.`id` = 1) [0m
[4;36;1manimal Load (0.1ms)[0m [0;1mSELECT `animals`.* FROM `animals` INNER JOIN `cages` ON `animals`.cage_id = `cages`.id WHERE ((`cages`.user_id = 1)) ORDER BY band[0m
[4;35;1manimal Columns (0.5ms)[0m [0mSHOW FIELDS FROM `animals`[0m
[4;36;1mCACHE (0.0ms)[0m [0;1mSELECT * FROM `users` WHERE (`users`.`id` = 1) [0m
[4;35;1mCACHE (0.0ms)[0m [0mSELECT `animals`.* FROM `animals` INNER JOIN `cages` ON `animals`.cage_id = `cages`.id WHERE ((`cages`.user_id = 1)) ORDER BY band[0m
[4;36;1mCACHE (0.0ms)[0m [0;1mSELECT * FROM `users` WHERE (`users`.`id` = 1) [0m
[4;35;1mCage Load (0.1ms)[0m [0mSELECT * FROM `cages` WHERE (date_destroyed is null) AND (`cages`.user_id = 1) ORDER BY name[0m
[4;36;1mCage Columns (0.4ms)[0m [0;1mSHOW FIELDS FROM `cages`[0m
[4;35;1mCACHE (0.0ms)[0m [0mSELECT * FROM `users` WHERE (`users`.`id` = 1) [0m
[4;36;1mCACHE (0.0ms)[0m [0;1mSELECT * FROM `cages` WHERE (date_destroyed is null) AND (`cages`.user_id = 1) ORDER BY name[0m
Rendered layouts/_menubar (87.9ms)
Rendered layouts/_menubar_login (103.2ms)
[4;35;1mCACHE (0.0ms)[0m [0mSELECT * FROM `users` WHERE (`users`.`id` = 1) [0m
[4;36;1mprotocols_users Columns (0.5ms)[0m [0;1mSHOW FIELDS FROM `protocols_users`[0m
[4;35;1mProtocol Load (0.1ms)[0m [0mSELECT * FROM `protocols` INNER JOIN `protocols_users` ON `protocols`.id = `protocols_users`.protocol_id WHERE (`protocols_users`.user_id = 1 ) ORDER BY number[0m
[4;36;1mProtocol Columns (0.4ms)[0m [0;1mSHOW FIELDS FROM `protocols`[0m
[4;35;1mTraining Load (0.1ms)[0m [0mSELECT * FROM `trainings` WHERE (`trainings`.user_id = 1) [0m
[4;36;1mCACHE (0.0ms)[0m [0;1mSELECT * FROM `users` WHERE (end_date is null) ORDER BY name[0m
[4;35;1mCACHE (0.0ms)[0m [0mSELECT * FROM `users` WHERE (end_date is null) ORDER BY name[0m
[4;36;1mCACHE (0.0ms)[0m [0;1mSELECT * FROM `users` WHERE (end_date is null and job_type regexp 'Anim') ORDER BY name[0m
[4;35;1mCACHE (0.0ms)[0m [0mSELECT * FROM `users` WHERE (end_date is null and job_type regexp 'Anim') ORDER BY name[0m
[4;36;1mCACHE (0.0ms)[0m [0;1mSELECT * FROM `users` WHERE (end_date is null and job_type regexp 'Medic') ORDER BY name[0m
[4;35;1mCACHE (0.0ms)[0m [0mSELECT * FROM `users` WHERE (end_date is null and job_type regexp 'Medic') ORDER BY name[0m
[4;36;1mCACHE (0.0ms)[0m [0;1mSELECT * FROM `users` WHERE (end_date is null and job_type regexp 'Weekend') ORDER BY name[0m
[4;35;1mCACHE (0.0ms)[0m [0mSELECT * FROM `users` WHERE (end_date is null and job_type regexp 'Weekend') ORDER BY name[0m
[4;36;1mUser Load (0.1ms)[0m [0;1mSELECT * FROM `users` WHERE (end_date is null and job_type regexp 'Admin') ORDER BY name[0m
[4;35;1mCACHE (0.0ms)[0m [0mSELECT * FROM `users` WHERE (end_date is null and job_type regexp 'Admin') ORDER BY name[0m
Rendered main/_sidebar (31.1ms)
Completed in 240ms (View: 197, DB: 3) | 200 OK [http://localhost/]
DEPRECATION WARNING: Giving :session_key to SessionStore is deprecated, please use :key instead. (called from new at /var/lib/gems/1.8/gems/actionpack-2.3.11/lib/action_controller/middleware_stack.rb:72)
[4;36;1mSQL (0.2ms)[0m [0;1mSET SQL_AUTO_IS_NULL=0[0m
Processing ProtocolsController#new (for 127.0.0.1 at 2011-04-12 18:11:14) [GET]
::: Checking session expiry
::: Session has not expired. Reinitialising.
::: Initializing session expiry. Expires at Tue Apr 12 18:26:14 -0400 2011
[4;35;1mProtocol Columns (0.5ms)[0m [0mSHOW FIELDS FROM `protocols`[0m
Rendering template within layouts/standard
Rendering protocols/new
[4;36;1mSpecies Load (0.1ms)[0m [0;1mSELECT * FROM `species` [0m
[4;35;1mSpecies Columns (0.4ms)[0m [0mSHOW FIELDS FROM `species`[0m
[4;36;1mAllowedanimal Load (0.3ms)[0m [0;1mSELECT * FROM `allowed_animals` WHERE (protocol_id = NULL and species_id = 70099589946800) LIMIT 1[0m
[4;35;1mAllowedanimal Load (0.1ms)[0m [0mSELECT * FROM `allowed_animals` WHERE (protocol_id = NULL and species_id = 70099593009960) LIMIT 1[0m
[4;36;1mAllowedanimal Load (0.1ms)[0m [0;1mSELECT * FROM `allowed_animals` WHERE (protocol_id = NULL and species_id = 70099592966760) LIMIT 1[0m
[4;35;1mAllowedanimal Load (0.1ms)[0m [0mSELECT * FROM `allowed_animals` WHERE (protocol_id = NULL and species_id = 70099592896700) LIMIT 1[0m
[4;36;1mUser Load (0.1ms)[0m [0;1mSELECT * FROM `users` WHERE (end_date is null) ORDER BY name[0m
[4;35;1mUser Columns (0.5ms)[0m [0mSHOW FIELDS FROM `users`[0m
[4;36;1mUser Load (0.1ms)[0m [0;1mSELECT `users`.id FROM `users` INNER JOIN `protocols_users` ON `users`.id = `protocols_users`.user_id WHERE (`users`.`id` = 33) AND (`protocols_users`.protocol_id = NULL ) ORDER BY name LIMIT 1[0m
[4;35;1mUser Load (0.1ms)[0m [0mSELECT `users`.id FROM `users` INNER JOIN `protocols_users` ON `users`.id = `protocols_users`.user_id WHERE (`users`.`id` = 38) AND (`protocols_users`.protocol_id = NULL ) ORDER BY name LIMIT 1[0m
[4;36;1mUser Load (0.0ms)[0m [0;1mSELECT `users`.id FROM `users` INNER JOIN `protocols_users` ON `users`.id = `protocols_users`.user_id WHERE (`users`.`id` = 21) AND (`protocols_users`.protocol_id = NULL ) ORDER BY name LIMIT 1[0m
[4;35;1mUser Load (0.1ms)[0m [0mSELECT `users`.id FROM `users` INNER JOIN `protocols_users` ON `users`.id = `protocols_users`.user_id WHERE (`users`.`id` = 1) AND (`protocols_users`.protocol_id = NULL ) ORDER BY name LIMIT 1[0m
[4;36;1mUser Load (0.0ms)[0m [0;1mSELECT `users`.id FROM `users` INNER JOIN `protocols_users` ON `users`.id = `protocols_users`.user_id WHERE (`users`.`id` = 14) AND (`protocols_users`.protocol_id = NULL ) ORDER BY name LIMIT 1[0m
[4;35;1mUser Load (0.0ms)[0m [0mSELECT `users`.id FROM `users` INNER JOIN `protocols_users` ON `users`.id = `protocols_users`.user_id WHERE (`users`.`id` = 37) AND (`protocols_users`.protocol_id = NULL ) ORDER BY name LIMIT 1[0m
[4;36;1mUser Load (0.0ms)[0m [0;1mSELECT `users`.id FROM `users` INNER JOIN `protocols_users` ON `users`.id = `protocols_users`.user_id WHERE (`users`.`id` = 31) AND (`protocols_users`.protocol_id = NULL ) ORDER BY name LIMIT 1[0m
[4;35;1mUser Load (0.0ms)[0m [0mSELECT `users`.id FROM `users` INNER JOIN `protocols_users` ON `users`.id = `protocols_users`.user_id WHERE (`users`.`id` = 39) AND (`protocols_users`.protocol_id = NULL ) ORDER BY name LIMIT 1[0m
[4;36;1mUser Load (0.1ms)[0m [0;1mSELECT `users`.id FROM `users` INNER JOIN `protocols_users` ON `users`.id = `protocols_users`.user_id WHERE (`users`.`id` = 5) AND (`protocols_users`.protocol_id = NULL ) ORDER BY name LIMIT 1[0m
[4;35;1mUser Load (0.0ms)[0m [0mSELECT `users`.id FROM `users` INNER JOIN `protocols_users` ON `users`.id = `protocols_users`.user_id WHERE (`users`.`id` = 18) AND (`protocols_users`.protocol_id = NULL ) ORDER BY name LIMIT 1[0m
[4;36;1mUser Load (0.0ms)[0m [0;1mSELECT `users`.id FROM `users` INNER JOIN `protocols_users` ON `users`.id = `protocols_users`.user_id WHERE (`users`.`id` = 32) AND (`protocols_users`.protocol_id = NULL ) ORDER BY name LIMIT 1[0m
[4;35;1mUser Load (0.0ms)[0m [0mSELECT `users`.id FROM `users` INNER JOIN `protocols_users` ON `users`.id = `protocols_users`.user_id WHERE (`users`.`id` = 9) AND (`protocols_users`.protocol_id = NULL ) ORDER BY name LIMIT 1[0m
[4;36;1mUser Load (0.0ms)[0m [0;1mSELECT `users`.id FROM `users` INNER JOIN `protocols_users` ON `users`.id = `protocols_users`.user_id WHERE (`users`.`id` = 40) AND (`protocols_users`.protocol_id = NULL ) ORDER BY name LIMIT 1[0m
[4;35;1mUser Load (0.0ms)[0m [0mSELECT `users`.id FROM `users` INNER JOIN `protocols_users` ON `users`.id = `protocols_users`.user_id WHERE (`users`.`id` = 6) AND (`protocols_users`.protocol_id = NULL ) ORDER BY name LIMIT 1[0m
Rendered protocols/_users_on_protocol_form (19.7ms)
Rendered protocols/_form (123.0ms)
Rendered layouts/_standard_head (2.4ms)
[4;36;1mUser Load (0.1ms)[0m [0;1mSELECT * FROM `users` WHERE (`users`.`id` = 1) [0m
[4;35;1mCACHE (0.0ms)[0m [0mSELECT * FROM `users` WHERE (`users`.`id` = 1) [0m
[4;36;1mCACHE (0.0ms)[0m [0;1mSELECT * FROM `users` WHERE (`users`.`id` = 1) [0m
[4;35;1mCACHE (0.0ms)[0m [0mSELECT * FROM `users` WHERE (`users`.`id` = 1) [0m
[4;36;1mCACHE (0.0ms)[0m [0;1mSELECT * FROM `users` WHERE (`users`.`id` = 1) [0m
[4;35;1mCACHE (0.0ms)[0m [0mSELECT * FROM `users` WHERE (`users`.`id` = 1) [0m
[4;36;1manimal Load (0.1ms)[0m [0;1mSELECT `animals`.* FROM `animals` INNER JOIN `cages` ON `animals`.cage_id = `cages`.id WHERE ((`cages`.user_id = 1)) ORDER BY band[0m
[4;35;1manimal Columns (0.6ms)[0m [0mSHOW FIELDS FROM `animals`[0m
[4;36;1mCACHE (0.0ms)[0m [0;1mSELECT * FROM `users` WHERE (`users`.`id` = 1) [0m
[4;35;1mCACHE (0.0ms)[0m [0mSELECT `animals`.* FROM `animals` INNER JOIN `cages` ON `animals`.cage_id = `cages`.id WHERE ((`cages`.user_id = 1)) ORDER BY band[0m
[4;36;1mCACHE (0.0ms)[0m [0;1mSELECT * FROM `users` WHERE (`users`.`id` = 1) [0m
[4;35;1mCage Load (0.1ms)[0m [0mSELECT * FROM `cages` WHERE (date_destroyed is null) AND (`cages`.user_id = 1) ORDER BY name[0m
[4;36;1mCage Columns (0.4ms)[0m [0;1mSHOW FIELDS FROM `cages`[0m
[4;35;1mCACHE (0.0ms)[0m [0mSELECT * FROM `users` WHERE (`users`.`id` = 1) [0m
[4;36;1mCACHE (0.0ms)[0m [0;1mSELECT * FROM `cages` WHERE (date_destroyed is null) AND (`cages`.user_id = 1) ORDER BY name[0m
Rendered layouts/_menubar (122.0ms)
Rendered layouts/_menubar_login (124.0ms)
Rendered layouts/_standard_layout (127.8ms)
Completed in 280ms (View: 266, DB: 4) | 200 OK [http://localhost/protocols/new]
DEPRECATION WARNING: Giving :session_key to SessionStore is deprecated, please use :key instead. (called from new at /var/lib/gems/1.8/gems/actionpack-2.3.11/lib/action_controller/middleware_stack.rb:72)
[4;35;1mSQL (0.1ms)[0m [0mSET SQL_AUTO_IS_NULL=0[0m
Processing ProtocolsController#create (for 127.0.0.1 at 2011-04-12 18:11:43) [POST]
Parameters: {"commit"=>"Save", "protocol"=>{"start_date(1i)"=>"2011", "number"=>"Test Number", "start_date(2i)"=>"4", "start_date(3i)"=>"12", "title"=>"Test Title", "allowed_animals_attributes"=>{"0"=>{"number"=>"7", "id"=>"2", "species_id"=>"2"}, "1"=>{"number"=>"11", "id"=>"3", "species_id"=>"3"}, "2"=>{"number"=>"13", "id"=>"4", "species_id"=>"4"}, "3"=>{"number"=>"17", "id"=>"5", "species_id"=>"5"}}, "flight_exempt"=>"0", "summary"=>"Test Summary"}, "user_id"=>{"6"=>"0", "33"=>"0", "9"=>"0", "14"=>"0", "37"=>"0", "38"=>"0", "40"=>"0", "39"=>"0", "1"=>"1", "18"=>"0", "31"=>"0", "32"=>"0", "21"=>"0", "5"=>"0"}}
::: Checking session expiry
::: Session has not expired. Reinitialising.
::: Initializing session expiry. Expires at Tue Apr 12 18:26:43 -0400 2011
[4;36;1mProtocol Columns (0.6ms)[0m [0;1mSHOW FIELDS FROM `protocols`[0m
ArgumentError (wrong number of arguments (0 for 1)):
app/controllers/protocols_controller.rb:42:in `new'
app/controllers/protocols_controller.rb:42:in `create'
/var/lib/gems/1.8/gems/ruby-debug-ide-0.4.9/lib/ruby-debug-ide.rb:109:in `debug_load'
/var/lib/gems/1.8/gems/ruby-debug-ide-0.4.9/lib/ruby-debug-ide.rb:109:in `debug_program'
/var/lib/gems/1.8/gems/ruby-debug-ide-0.4.9/bin/rdebug-ide:87
/var/lib/gems/1.8/bin/rdebug-ide:19:in `load'
/var/lib/gems/1.8/bin/rdebug-ide:19
Rendered rescues/_trace (46.0ms)
Rendered rescues/_request_and_response (1.7ms)
Rendering rescues/layout (internal_server_error)
我发现问题是我在协议模型中有一个函数与我试图嵌套的模型(AllowedAnimal)同名。调用协议时。它正在调用函数(没有足够的参数),而不是返回嵌套的字段。
我还必须将允许的动物模型中的关系更改为belongs_to以获得我的allowed_animal。工作物种:
class AllowedAnimal < ActiveRecord::Base
belongs_to :protocol
belongs_to :species
end
并在我的模型中添加一个方法来构建一个新协议的allowed_animals:
def build_allowed_animals
abs = Array.new
for sp in Species.all
ab = AllowedAnimal.new
ab.species = sp
ab.number = 0
abs << ab
end
self.allowed_animals = abs
end
调用协议控制器中的方法:
def new
@protocol = Protocol.new
@protocol.build_allowed_bats
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @protocol }
end
end
并对我的表单进行一些编辑:
<% form_for(@protocol) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.label :number %><br />
<%= f.text_field :number %>
</p>
<p>
Number of animals allowed on protocol:
</p>
<div class="left_indent">
<table>
<% f.fields_for :allowed_animals do |a| %>
<tr>
<td><i><%= a.object.species.name %></i></td>
<td><%= a.text_field :number, :value => @protocol.determine_allowed_animals(a.object.species), :size=>2 %></td>
<%= a.text_field :species_id, :value => a.object.species.id, :hidden => true %>
</tr>
<% end %>
</table>
</div>
<p>
Users on protocol:
</p>
<div class="left_indent">
<%= render :partial => 'protocols/users_on_protocol_form', :locals =>{:protocol => @protocol, :users => User.current} %>
</div>
<p>
<%= f.submit 'Save' %>
</p>
<% end %>
在rails 3中,你应该这样写:
<%= f.fields_for :allowed_animals do |a| %>