fields_for 和参数数量错误(0 代表 1)错误

发布于 2024-10-31 10:14:30 字数 19318 浏览 6 评论 0原文

业余 Rails 程序员努力使用 fields_for 表单生成器。我的 Rails 应用程序(v=2.3.11)跟踪实验室协议和这些协议中使用的动物(它们的体重等)。

我的具体问题是协议只能有一定数量的动物,但允许的数量是特定于某个物种的。

嵌套表单行为应显示所有动物物种的列表以及协议允许的数量的编辑框。

模型:协议、Allowed_Animal、物种

物种模型

class Species < ActiveRecord::Base
  has_many :animals, :order => "band"
  has_many :allowed_animals
end

协议模型

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 模型

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)

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).

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

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

class AllowedAnimal < ActiveRecord::Base
  belongs_to :protocol
  has_one :species
end

The table for allowed_animal includes:

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?

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)

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

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

发布评论

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

评论(2

梦途 2024-11-07 10:14:30

我发现问题是我的协议模型中有一个与我试图嵌套的模型同名的函数(AllowedAnimal)。当调用protocol.allowed_animals时,它正在调用函数(没有足够的参数)而不是返回嵌套的fields_for。

我还必须在我的允许的动物模型中更改与belongs_to的关系以使我的allowed_animal.species正常工作:

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

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.

I also had to change the relationship to belongs_to in my Allowed Animal Model to get my allowed_animal.species to work:

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:

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 %>
任性一次 2024-11-07 10:14:30

在 Rails 3 中,你应该这样写:

<%= f.fields_for :allowed_animals do |a| %>

In rails 3, You should just write:

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