Amateur rails programmer struggling to use the fields_for form builder. My rails application (on v=2.3.11) tracks laboratory protocols and the animals used on those protocols (their weights, etc).
难以使用fields_for表单构建器的业余rails程序员。我的rails应用程序(on v=2.3.11)跟踪实验室协议和在这些协议上使用的动物(它们的重量等)。
My specific problem is that protocols can only have a certain number of animals, but that number allowed is specific to a species.
我的具体问题是协议只能有一定数量的动物,但是这个数量是特定于一个物种的。
The nested form behavior should display a listing of all the species of animals and an edit box for the number allowed on the protocol.
嵌套的表单行为应该显示所有动物种类的列表,以及在协议中允许的数量的编辑框。
Models: Protocol, Allowed_Animal, Species
模型:协议、Allowed_Animal物种
Species Model
物种模型
class Species < ActiveRecord::Base
has_many :animals, :order => "band"
has_many :allowed_animals
end
Protocol Model
协议模型
class Protocol < ActiveRecord::Base
has_and_belongs_to_many :animals, :order => "band"
has_many :allowed_animals
accepts_nested_attributes_for :allowed_animals
end
Allowed_Animal Model
Allowed_Animal模型
class AllowedAnimal < ActiveRecord::Base
belongs_to :protocol
has_one :species
end
The table for allowed_animal includes:
allowed_animal的表包括:
id, protocol_id, species_id, number, created_at, updated_at
The nested form works like this:
嵌套的表单如下:
<% 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 %>
The form displays how I would like it to, but after submission, I get the error:
表单显示了我想要的方式,但是提交后,我得到了错误:
wrong number of arguments (0 for 1)
in the controller as it tries to do:
在控制器中
@protocol = Protocol.new(params[:protocol])
I am not sure where to go from here. Do I need to have a build method in my model? Or do I need to do anything special in my controller? Is there something wrong with my form_builder code and how it iterates through the species?
我不知道从这里到哪里去。我需要在我的模型中有一个构建方法吗?或者我需要在控制器中做任何特别的事情?我的form_builder代码有什么问题吗?
Any ideas?
什么好主意吗?
Application trace:
应用程序跟踪:
/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'
Parameters:
参数:
{"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"}}
Development Log
开发日志
# 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)
2 个解决方案
#1
1
I figured out that the problem was that I had a function in my Protocol model with the same name as the model I was trying to nest (AllowedAnimal). When calling protocol.allowed_animals it was calling the function (with not enough arguments) instead of returning the nested fields_for.
我发现问题在于,我的协议模型中有一个函数,其名称与我试图嵌套的模型(AllowedAnimal)相同。当调用协议。allowed_animals它调用函数(参数不足)而不是返回嵌套的fields_for。
I also had to change the relationship to belongs_to in my Allowed Animal Model to get my allowed_animal.species to work:
我还必须在允许的动物模型中改变belongs_to的关系,才能获得允许的动物。物种的工作:
class AllowedAnimal < ActiveRecord::Base
belongs_to :protocol
belongs_to :species
end
And add a method in my model to build out the allowed_animals for a new protocol:
在我的模型中添加一个方法来构建一个新协议的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
Call the method in my Protocol Controller:
在我的协议控制器中调用该方法:
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
And make a few edits to my form:
并对我的表格做一些修改:
<% 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 %>
#2
0
In rails 3, You should just write:
在rails 3中,您应该写:
<%= f.fields_for :allowed_animals do |a| %>
#1
1
I figured out that the problem was that I had a function in my Protocol model with the same name as the model I was trying to nest (AllowedAnimal). When calling protocol.allowed_animals it was calling the function (with not enough arguments) instead of returning the nested fields_for.
我发现问题在于,我的协议模型中有一个函数,其名称与我试图嵌套的模型(AllowedAnimal)相同。当调用协议。allowed_animals它调用函数(参数不足)而不是返回嵌套的fields_for。
I also had to change the relationship to belongs_to in my Allowed Animal Model to get my allowed_animal.species to work:
我还必须在允许的动物模型中改变belongs_to的关系,才能获得允许的动物。物种的工作:
class AllowedAnimal < ActiveRecord::Base
belongs_to :protocol
belongs_to :species
end
And add a method in my model to build out the allowed_animals for a new protocol:
在我的模型中添加一个方法来构建一个新协议的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
Call the method in my Protocol Controller:
在我的协议控制器中调用该方法:
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
And make a few edits to my form:
并对我的表格做一些修改:
<% 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 %>
#2
0
In rails 3, You should just write:
在rails 3中,您应该写:
<%= f.fields_for :allowed_animals do |a| %>