Before I start,  let me mention that I have posted a Microsoft Word
document of the following response, along with two relevant files of
the app here:
http://www.filefactory.com/file/b29ea4f/n/Problem_writing_out_a_new_expense_record.doc
http://www.filefactory.com/file/b29ea3c/n/new.html.erb
http://www.filefactory.com/file/b29ea2c/n/expenses_controller.rb
You may wish to download some or all for reference purposes of ease of
reading.

> Are you absolutely certain that the trace you show is as a result of the code 
> you have posted?
No.  When I originally ran the app (and when I ran it just now),  I
had expense\new.html.erb set with:
   <%# New version of vendor selection -%>
    <% @vendors = Vendor.find( :all, :order=>"nickname ASC") -%>
    <%= f.collection_select(:vendor_id, @vendors, :id, :nickname) %>
    <%# NOTE: first argument, symbol :vendor, does not produce an
error here either -%>
    <%# End of New version -%>

Because of “vendor_id” in the collection_select, I get;
NoMethodError in Expenses#new
Showing app/views/expenses/new.html.erb where line #13 raised:
undefined method `vendor_id' for #<Expense:0x47034fc>
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:     <%# NOTE: first argument, symbol :vendor, does not produce an
error here either -%>
15:     <%# End of New version -%>
16:

But I had originally run it with (and just now changed it to):

    <%# New version of vendor selection -%>
    <% @vendors = Vendor.find( :all, :order=>"nickname ASC") -%>
    <%= f.collection_select(:vendor, @vendors, :id, :nickname) %>
    <%# NOTE: first argument, symbol :vendor, does not produce an
error here -%>
    <%# End of New version -%>
 where I went back to :vendor (and removed “also” from my comment.

I had changed to vendor_id because that’s what Frederick Cheung
suggested, but I apparently changed it after I encountered the error
in the Expense controller.

Now with the change back to :vendor and clicking the “Reload current
page” icon in Firefox,  the Expense\new.html.erb pages is displayed
without error.  From there I click the down arrow in the item
immediately below the Vendor tag and select the item with the vendor
nickname “vendor 7/12/2010-1623” and populate the rest of the fields.

Then when I click Create, I get the error display:
ActiveRecord::AssociationTypeMismatch in ExpensesController#create
Vendor(#37496880) expected, got String(#21132310)
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:45:in `new'
K:/_Projects/Ruby/_Rails_Apps/_EIMS/RTS/app/controllers/
expenses_controller.rb:45:in `create'
Request
Parameters:
{"commit"=>"Create",
 "expense"=>{"category"=>"cat",
 "account"=>"acc",
 "mode"=>"cash",
 "tran_date(1i)"=>"2010",
 "tran_date(2i)"=>"7",
 "description"=>"7/12/2010-1623",
 "amount"=>"12.34",
 "tran_date(3i)"=>"18",
 "vendor"=>"65",
 "user_id"=>"rlm"},
 "authenticity_token"=>"p0Vg6N2ZxXK/A9d2guWtlmqqsWYo5yj6Ey2+hgmPGCs="}
Show session dump
---
Response
Headers:
{"Content-Type"=>"",
 "Cache-Control"=>"no-cache"}

For some reason,  this above dump does not display line 45 nor its
context.  But the relevant portion of app\controllers
\expenses_controller.rb is shown here:
  # POST /expenses
  # POST /expenses.xml
  def create
    debugger
    @expense = Expense.new(params[:expense])  # <== Line 45
*****************

    respond_to do |format|
      if @expense.save
        flash[:notice] = 'Expense was successfully created.'
        format.html { redirect_to(@expense) }
        format.xml  { render :xml => @expense, :status
=> :created, :location => @expense }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @expense.errors, :status
=> :unprocessable_entity }
      end
    end
  end

I note that the params["expense"]["vendor"]=>"65", which I view as
almost perfect, i.e. it’s a vendor ID as opposed to a vendor
nickname.  But is it the ID matching my selection?

It is indeed, as witnessed by the fact:
mysql> select * from vendors;
+----+-----------------------+----------------------------------
+---------------------+---------------------+
| id | nickname              | qbname                           |
created_at          | updated_at          |
+----+-----------------------+----------------------------------
+---------------------+---------------------+
| 62 | vendor 5/15/2010      | Test new field names             |
2010-05-15 16:30:11 | 2010-05-15 16:30:11 |
| 63 | vendor 5/21/2010      | Testing pre-plugin version       |
2010-05-21 19:38:36 | 2010-05-21 19:38:36 |
| 64 | Z01-2010.06.26        | Z01-Test                         |
2010-06-26 14:28:11 | 2010-06-26 14:28:11 |
| 65 | vendor 7/12/2010-1623 | Just a test of "belongs_to", etc |
2010-07-12 20:25:05 | 2010-07-12 20:35:11 |
+----+-----------------------+----------------------------------
+---------------------+---------------------+

I hope you can see what's wrong here.  Maybe I should just rebuild the
Expense portion in its entirety, now that I'm so close to getting the
ID of a selected vendor to appear ready to be saved in new expense
records.

Best wishes,
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.

Reply via email to