On 17 July 2010 15:50, RichardOnRails <richarddummymailbox58...@uscomputergurus.com> wrote: > Hi All, > > I got closer. I tried: > > <% @vendors = Vendor.find( :all, :order=>"nickname ASC") -%> > <%= f.collection_select(:vendor, @vendors, :id, :nickname) %> > > I ran the app and created a new expense. I clicked the drop-down list, > selected a vendor, populated the remaining fields of the form and > saved the expense. The app then crashed (that's the bad part), *but* > the Request parameters were perfect, specifically: > "vendor"=>"64" > > So I saw the selected vendor's name when I clicked Save/Submit/ > whatever and the generated expense record contained the id of the > selected vendor, not the name of that vendor, as had been the case in > some earlier versions of my code. > > Below are some details of this crash. > > Thanks for any help you may offer in resolving this matter. > -- > Richard > > ActiveRecord::AssociationTypeMismatch in ExpensesController#create > > Vendor(#37038620) expected, got String(#21132310)
It is always worth while trying to understand error messages, quite often they can give you a clue as the problem. The error above says that a Vendor object was expected, but you have given it a string. >From the trace below you can see that it is at line 44 of expenses_controller.rb. Have a look at that line and see if you can work out why it is expecting a Vendor object, see what you have actually provided and see if you understand what is going on. Colin > > RAILS_ROOT: K:/_Projects/Ruby/_Rails_Apps/_EIMS/RTS > Application Trace | Framework Trace | Full Trace > > K:/_Utilities/ruby186-26_rc2/ruby/lib/ruby/gems/1.8/gems/ > activerecord-2.3.5/lib/active_record/associations/association_proxy.rb: > 262:in `raise_on_type_mismatch' > K:/_Utilities/ruby186-26_rc2/ruby/lib/ruby/gems/1.8/gems/ > activerecord-2.3.5/lib/active_record/associations/ > belongs_to_association.rb:22:in `replace' > K:/_Utilities/ruby186-26_rc2/ruby/lib/ruby/gems/1.8/gems/ > activerecord-2.3.5/lib/active_record/associations.rb:1281:in `vendor=' > K:/_Utilities/ruby186-26_rc2/ruby/lib/ruby/gems/1.8/gems/ > activerecord-2.3.5/lib/active_record/base.rb:2746:in `send' > K:/_Utilities/ruby186-26_rc2/ruby/lib/ruby/gems/1.8/gems/ > activerecord-2.3.5/lib/active_record/base.rb:2746:in `attributes=' > K:/_Utilities/ruby186-26_rc2/ruby/lib/ruby/gems/1.8/gems/ > activerecord-2.3.5/lib/active_record/base.rb:2742:in `each' > K:/_Utilities/ruby186-26_rc2/ruby/lib/ruby/gems/1.8/gems/ > activerecord-2.3.5/lib/active_record/base.rb:2742:in `attributes=' > K:/_Utilities/ruby186-26_rc2/ruby/lib/ruby/gems/1.8/gems/ > activerecord-2.3.5/lib/active_record/base.rb:2438:in `initialize' > K:/_Projects/Ruby/_Rails_Apps/_EIMS/RTS/app/controllers/ > expenses_controller.rb:44:in `new' > K:/_Projects/Ruby/_Rails_Apps/_EIMS/RTS/app/controllers/ > expenses_controller.rb:44:in `create' > > > > > On Jul 17, 9:24 am, RichardOnRails > <richarddummymailbox58...@uscomputergurus.com> wrote: >> Hi Fred, >> >> Thanks for taking the trouble to respond again, especially since I >> failed to follow your previous response. The problem then was that I >> was unsuccessful in applying it or didn't understand it ... probable >> both. I definitely failed to realize that the argument (object) was >> to be omitted in the context of form elements. That "little detail" >> seems missing in ActionView::Helpers::FormOptionsHelper. Someone >> explicitly mentioned that in a subsequent post on this thread. >> >> Sadly, when I followed your suggestion "to the letter", I was >> unsuccessful again. I've got: >> ====== Error Msg ========= >> NameError in Expenses#new >> >> Showing app/views/expenses/new.html.erb where line #13 raised: >> >> undefined local variable or method `vendor_id' for #<ActionView::Base: >> 0x4822310> >> >> Extracted source (around line #13): >> >> 10: >> 11: <%# New version of vendor selection -%> >> 12: <% @vendors = Vendor.find( :all, :order=>"nickname ASC") -%> >> 13: <%= f.collection_select(vendor_id, @vendors, :id, :nickname) >> %> >> 14: <%# End of New version -%> >> [snip] >> ====== Error Msg ========= >> >> So I tested whether vendor_id would be defined if I created an Expense >> instance in Rails console: Here's what I got (edited for brevity): >> K:\_Projects\Ruby\_Rails_Apps\_EIMS\RTS>ruby script/console >> Loading development environment (Rails 2.3.5)>> expense = Expense.new >> >> => #<Expense id: nil, vendor: nil, description: nil, category: nil, >> [snip] >> >> >> expense.vendor_id [NoMethodError] >> >> Expense.vendor_id [NoMethodError] >> >> params[:vendor_id] [NameError] >> >> I also tried using the symbol :vendor_id as the first arg. That >> failed, too. >> >> I apologize for being so obtuse. But I'd really like to get this app >> working, and getting this expense-vendor hookup working may be my >> last obstacle (for a while, anyway). >> >> If you can give me a little more help about this problem, I'd be most >> appreciative. If you need more info, I'd be happy to post portions >> or all my code to a website from which you could download it for >> inspection. >> >> Best wishes, >> Richard >> >> On Jul 17, 5:29 am, Frederick Cheung <frederick.che...@gmail.com> >> wrote: >> >> > On Jul 16, 10:40 pm, RichardOnRails >> >> > <richarddummymailbox58...@uscomputergurus.com> wrote: >> > > Hey Colin, >> >> > > Here's one more detail I should have added from my new-expense-view >> > > code: >> >> > > <%= f.label :vendor %><br /> >> >> > > <%# New version of vendor selection -%> >> > > <% params[:expense] = 10 -%> >> > > <% @vendors = Vendor.find( :all, :order=>"nickname ASC") -%> >> > > <%= f.collection_select(:id, @vendors, :id, :nickname) %> >> >> > because the first argument should be the name of the attribute you are >> > trying to set (ie vendor_id). This and f.collection_select not needing >> > the first argument was on the link I gave on one of your other posts >> > (http://guides.rubyonrails.org/form_helpers.html) >> >> > Fred >> >> > > <%# End of New version -%> >> >> > > I apologize for the fragmented response. I was in a hurry to go out >> > > for my 3-mile walk :-) >> >> > > Best again, >> > > Richard > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-t...@googlegroups.com. > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.