Topic: More Select Woes

okay I seem to be doing everything wrong with selects in Rails

I ran into a problem with selecting stuff that was refering to a forign key, the error givin was "expected model got  string" I did some hunting on the web and found that adding _id would solve this problem, it did and caused another one. Rails is nw telling me that "nil object while evaluating nil.inject?", pift I say.

[code html]
    <dt>Budget</dt>
    <dd><%= form.collection_select(:project_id, @projects, :id, :name, :prompt => "Choose a Project") %></dd>
[/code]

[code model]
   belongs_to :project
[/code]

[code controller]
   @projects = Project.find(:all, :conditions => ["client_id = ?", @user.client_id])
[/code]

--

Re: More Select Woes

Can you post the full error message with the stack trace?

Railscasts - Free Ruby on Rails Screencasts

Re: More Select Woes

sorry


NoMethodError in Manage#create

Showing app/views/manage/new_transaction.rhtml where line #18 raised:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.inject
Extracted source (around line #18):

15:     <dd><%= form.text_field :amount, :size => 20 %></dd>
16:
17:     <dt>Budget</dt>
18:     <dd><%= form.collection_select(:budget_id, @budgets, :id, :name, :prompt => "Choose a Budget") %></dd>
19:    
20:     <dt>Account</dt>
21:     <dd><%= form.collection_select(:account_id, @accounts, :id, :name, :prompt => "Choose a Account") %></dd>
RAILS_ROOT: script/../config/..

Application Trace | Framework Trace | Full Trace
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action_view/helpers/form_options_helper.rb:142:in `options_from_collection_for_select'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action_view/helpers/form_options_helper.rb:310:in `to_collection_select_tag'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action_view/helpers/form_options_helper.rb:71:in `collection_select'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action_view/helpers/form_options_helper.rb:351:in `collection_select'
#{RAILS_ROOT}/app/views/manage/new_transaction.rhtml:18:in `_run_rhtml_47app47views47manage47new_transaction46rhtml'
#{RAILS_ROOT}/app/views/manage/new_transaction.rhtml:3:in `fields_for'
#{RAILS_ROOT}/app/views/manage/new_transaction.rhtml:3:in `_run_rhtml_47app47views47manage47new_transaction46rhtml'
app/controllers/manage_controller.rb:69:in `save_new'
app/controllers/manage_controller.rb:55:in `create'

Last edited by BasicMind (2007-01-28 18:54:34)

--

Re: More Select Woes

Are you certain @budgets is set in both the "new" and "create" actions? If the validation fails the new action isn't called again, but the new template is rendered, therefore you need to set the @budgets (and @accounts) and any other instance variables used in the view in the "create" action.

Railscasts - Free Ruby on Rails Screencasts

Re: More Select Woes

ah I see. so render :action=> :whatever will not render the instance variables again? is there a way to force it to.

--

Re: More Select Woes

Nope to both. render just re-renders the template, there's no way to call the "new" action again. You'll need to reset the variables in "create".

Railscasts - Free Ruby on Rails Screencasts