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_动物模型

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

2 回答 2

1

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

我还必须在Allowed Animal Model中更改与 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 %>
于 2011-04-14T20:36:29.740 回答
0

在 rails 3 中,您应该只写:

<%= f.fields_for :allowed_animals do |a| %>
于 2011-04-11T23:42:16.803 回答