ruby on rails - fields_for and wrong number of arguments (0 for 1) error -
amateur rails programmer struggling use fields_for form builder. rails application (on v=2.3.11) tracks laboratory protocols , animals used on protocols (their weights, etc).
my specific problem protocols can have number of animals, number allowed specific species.
the nested form behavior should display listing of species of animals , edit box number allowed on 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 allowed_animal includes:
id, protocol_id, species_id, number, created_at, updated_at the nested form works this:
<% form_for(@protocol) |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 |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 to, after submission, error:
wrong number of arguments (0 1) in controller tries do:
@protocol = protocol.new(params[:protocol]) i not sure go here. need have build method in model? or need special in controller? there wrong form_builder code , how iterates through 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 sessionstore deprecated, please use :key instead. (called new @ /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 @ 2011-04-12 18:11:12) [get] ::: checking session expiry ::: session has not expired. reinitialising. ::: initializing session expiry. expires @ tue apr 12 18:26:12 -0400 2011 [4;35;1muser load (0.1ms)[0m [0mselect * `users` (end_date null) order name[0m [4;36;1muser load (0.1ms)[0m [0;1mselect * `users` (end_date null , job_type regexp 'weekend') order name[0m [4;35;1muser load (0.1ms)[0m [0mselect * `users` (end_date null , job_type regexp 'medic') order name[0m [4;36;1muser load (0.2ms)[0m [0;1mselect * `users` (end_date null , job_type regexp 'anim') order 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 `users`[0m [4;36;1muser load (0.1ms)[0m [0;1mselect * `users` (`users`.`id` = 1) [0m [4;35;1mcache (0.0ms)[0m [0mselect * `users` (`users`.`id` = 1) [0m [4;36;1mcache (0.0ms)[0m [0;1mselect * `users` (`users`.`id` = 1) [0m [4;35;1mcache (0.0ms)[0m [0mselect * `users` (`users`.`id` = 1) [0m [4;36;1mcache (0.0ms)[0m [0;1mselect * `users` (`users`.`id` = 1) [0m [4;35;1mcache (0.0ms)[0m [0mselect * `users` (`users`.`id` = 1) [0m [4;36;1manimal load (0.1ms)[0m [0;1mselect `animals`.* `animals` inner join `cages` on `animals`.cage_id = `cages`.id ((`cages`.user_id = 1)) order band[0m [4;35;1manimal columns (0.5ms)[0m [0mshow fields `animals`[0m [4;36;1mcache (0.0ms)[0m [0;1mselect * `users` (`users`.`id` = 1) [0m [4;35;1mcache (0.0ms)[0m [0mselect `animals`.* `animals` inner join `cages` on `animals`.cage_id = `cages`.id ((`cages`.user_id = 1)) order band[0m [4;36;1mcache (0.0ms)[0m [0;1mselect * `users` (`users`.`id` = 1) [0m [4;35;1mcage load (0.1ms)[0m [0mselect * `cages` (date_destroyed null) , (`cages`.user_id = 1) order name[0m [4;36;1mcage columns (0.4ms)[0m [0;1mshow fields `cages`[0m [4;35;1mcache (0.0ms)[0m [0mselect * `users` (`users`.`id` = 1) [0m [4;36;1mcache (0.0ms)[0m [0;1mselect * `cages` (date_destroyed null) , (`cages`.user_id = 1) order name[0m rendered layouts/_menubar (87.9ms) rendered layouts/_menubar_login (103.2ms) [4;35;1mcache (0.0ms)[0m [0mselect * `users` (`users`.`id` = 1) [0m [4;36;1mprotocols_users columns (0.5ms)[0m [0;1mshow fields `protocols_users`[0m [4;35;1mprotocol load (0.1ms)[0m [0mselect * `protocols` inner join `protocols_users` on `protocols`.id = `protocols_users`.protocol_id (`protocols_users`.user_id = 1 ) order number[0m [4;36;1mprotocol columns (0.4ms)[0m [0;1mshow fields `protocols`[0m [4;35;1mtraining load (0.1ms)[0m [0mselect * `trainings` (`trainings`.user_id = 1) [0m [4;36;1mcache (0.0ms)[0m [0;1mselect * `users` (end_date null) order name[0m [4;35;1mcache (0.0ms)[0m [0mselect * `users` (end_date null) order name[0m [4;36;1mcache (0.0ms)[0m [0;1mselect * `users` (end_date null , job_type regexp 'anim') order name[0m [4;35;1mcache (0.0ms)[0m [0mselect * `users` (end_date null , job_type regexp 'anim') order name[0m [4;36;1mcache (0.0ms)[0m [0;1mselect * `users` (end_date null , job_type regexp 'medic') order name[0m [4;35;1mcache (0.0ms)[0m [0mselect * `users` (end_date null , job_type regexp 'medic') order name[0m [4;36;1mcache (0.0ms)[0m [0;1mselect * `users` (end_date null , job_type regexp 'weekend') order name[0m [4;35;1mcache (0.0ms)[0m [0mselect * `users` (end_date null , job_type regexp 'weekend') order name[0m [4;36;1muser load (0.1ms)[0m [0;1mselect * `users` (end_date null , job_type regexp 'admin') order name[0m [4;35;1mcache (0.0ms)[0m [0mselect * `users` (end_date null , job_type regexp 'admin') order name[0m rendered main/_sidebar (31.1ms) completed in 240ms (view: 197, db: 3) | 200 ok [http://localhost/] deprecation warning: giving :session_key sessionstore deprecated, please use :key instead. (called new @ /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 @ 2011-04-12 18:11:14) [get] ::: checking session expiry ::: session has not expired. reinitialising. ::: initializing session expiry. expires @ tue apr 12 18:26:14 -0400 2011 [4;35;1mprotocol columns (0.5ms)[0m [0mshow fields `protocols`[0m rendering template within layouts/standard rendering protocols/new [4;36;1mspecies load (0.1ms)[0m [0;1mselect * `species` [0m [4;35;1mspecies columns (0.4ms)[0m [0mshow fields `species`[0m [4;36;1mallowedanimal load (0.3ms)[0m [0;1mselect * `allowed_animals` (protocol_id = null , species_id = 70099589946800) limit 1[0m [4;35;1mallowedanimal load (0.1ms)[0m [0mselect * `allowed_animals` (protocol_id = null , species_id = 70099593009960) limit 1[0m [4;36;1mallowedanimal load (0.1ms)[0m [0;1mselect * `allowed_animals` (protocol_id = null , species_id = 70099592966760) limit 1[0m [4;35;1mallowedanimal load (0.1ms)[0m [0mselect * `allowed_animals` (protocol_id = null , species_id = 70099592896700) limit 1[0m [4;36;1muser load (0.1ms)[0m [0;1mselect * `users` (end_date null) order name[0m [4;35;1muser columns (0.5ms)[0m [0mshow fields `users`[0m [4;36;1muser load (0.1ms)[0m [0;1mselect `users`.id `users` inner join `protocols_users` on `users`.id = `protocols_users`.user_id (`users`.`id` = 33) , (`protocols_users`.protocol_id = null ) order name limit 1[0m [4;35;1muser load (0.1ms)[0m [0mselect `users`.id `users` inner join `protocols_users` on `users`.id = `protocols_users`.user_id (`users`.`id` = 38) , (`protocols_users`.protocol_id = null ) order name limit 1[0m [4;36;1muser load (0.0ms)[0m [0;1mselect `users`.id `users` inner join `protocols_users` on `users`.id = `protocols_users`.user_id (`users`.`id` = 21) , (`protocols_users`.protocol_id = null ) order name limit 1[0m [4;35;1muser load (0.1ms)[0m [0mselect `users`.id `users` inner join `protocols_users` on `users`.id = `protocols_users`.user_id (`users`.`id` = 1) , (`protocols_users`.protocol_id = null ) order name limit 1[0m [4;36;1muser load (0.0ms)[0m [0;1mselect `users`.id `users` inner join `protocols_users` on `users`.id = `protocols_users`.user_id (`users`.`id` = 14) , (`protocols_users`.protocol_id = null ) order name limit 1[0m [4;35;1muser load (0.0ms)[0m [0mselect `users`.id `users` inner join `protocols_users` on `users`.id = `protocols_users`.user_id (`users`.`id` = 37) , (`protocols_users`.protocol_id = null ) order name limit 1[0m [4;36;1muser load (0.0ms)[0m [0;1mselect `users`.id `users` inner join `protocols_users` on `users`.id = `protocols_users`.user_id (`users`.`id` = 31) , (`protocols_users`.protocol_id = null ) order name limit 1[0m [4;35;1muser load (0.0ms)[0m [0mselect `users`.id `users` inner join `protocols_users` on `users`.id = `protocols_users`.user_id (`users`.`id` = 39) , (`protocols_users`.protocol_id = null ) order name limit 1[0m [4;36;1muser load (0.1ms)[0m [0;1mselect `users`.id `users` inner join `protocols_users` on `users`.id = `protocols_users`.user_id (`users`.`id` = 5) , (`protocols_users`.protocol_id = null ) order name limit 1[0m [4;35;1muser load (0.0ms)[0m [0mselect `users`.id `users` inner join `protocols_users` on `users`.id = `protocols_users`.user_id (`users`.`id` = 18) , (`protocols_users`.protocol_id = null ) order name limit 1[0m [4;36;1muser load (0.0ms)[0m [0;1mselect `users`.id `users` inner join `protocols_users` on `users`.id = `protocols_users`.user_id (`users`.`id` = 32) , (`protocols_users`.protocol_id = null ) order name limit 1[0m [4;35;1muser load (0.0ms)[0m [0mselect `users`.id `users` inner join `protocols_users` on `users`.id = `protocols_users`.user_id (`users`.`id` = 9) , (`protocols_users`.protocol_id = null ) order name limit 1[0m [4;36;1muser load (0.0ms)[0m [0;1mselect `users`.id `users` inner join `protocols_users` on `users`.id = `protocols_users`.user_id (`users`.`id` = 40) , (`protocols_users`.protocol_id = null ) order name limit 1[0m [4;35;1muser load (0.0ms)[0m [0mselect `users`.id `users` inner join `protocols_users` on `users`.id = `protocols_users`.user_id (`users`.`id` = 6) , (`protocols_users`.protocol_id = null ) order 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 * `users` (`users`.`id` = 1) [0m [4;35;1mcache (0.0ms)[0m [0mselect * `users` (`users`.`id` = 1) [0m [4;36;1mcache (0.0ms)[0m [0;1mselect * `users` (`users`.`id` = 1) [0m [4;35;1mcache (0.0ms)[0m [0mselect * `users` (`users`.`id` = 1) [0m [4;36;1mcache (0.0ms)[0m [0;1mselect * `users` (`users`.`id` = 1) [0m [4;35;1mcache (0.0ms)[0m [0mselect * `users` (`users`.`id` = 1) [0m [4;36;1manimal load (0.1ms)[0m [0;1mselect `animals`.* `animals` inner join `cages` on `animals`.cage_id = `cages`.id ((`cages`.user_id = 1)) order band[0m [4;35;1manimal columns (0.6ms)[0m [0mshow fields `animals`[0m [4;36;1mcache (0.0ms)[0m [0;1mselect * `users` (`users`.`id` = 1) [0m [4;35;1mcache (0.0ms)[0m [0mselect `animals`.* `animals` inner join `cages` on `animals`.cage_id = `cages`.id ((`cages`.user_id = 1)) order band[0m [4;36;1mcache (0.0ms)[0m [0;1mselect * `users` (`users`.`id` = 1) [0m [4;35;1mcage load (0.1ms)[0m [0mselect * `cages` (date_destroyed null) , (`cages`.user_id = 1) order name[0m [4;36;1mcage columns (0.4ms)[0m [0;1mshow fields `cages`[0m [4;35;1mcache (0.0ms)[0m [0mselect * `users` (`users`.`id` = 1) [0m [4;36;1mcache (0.0ms)[0m [0;1mselect * `cages` (date_destroyed null) , (`cages`.user_id = 1) order 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 sessionstore deprecated, please use :key instead. (called new @ /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 @ 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 @ tue apr 12 18:26:43 -0400 2011 [4;36;1mprotocol columns (0.6ms)[0m [0;1mshow fields `protocols`[0m argumenterror (wrong number of arguments (0 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)
i figured out problem had function in protocol model same name model trying nest (allowedanimal). when calling protocol.allowed_animals calling function (with not enough arguments) instead of returning nested fields_for.
i had change relationship belongs_to in allowed animal model allowed_animal.species work:
class allowedanimal < activerecord::base belongs_to :protocol belongs_to :species end and add method in model build out allowed_animals new protocol:
def build_allowed_animals abs = array.new sp in species.all ab = allowedanimal.new ab.species = sp ab.number = 0 abs << ab end self.allowed_animals = abs end call method in protocol controller:
def new @protocol = protocol.new @protocol.build_allowed_bats respond_to |format| format.html # new.html.erb format.xml { render :xml => @protocol } end end and make few edits form:
<% form_for(@protocol) |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 |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 %>
Comments
Post a Comment