select helper

How do I amend the form helper:

<%= select("lead", "associate_id", Associate.all.collect {|p| [ p.to_s, p.id ] }, {:include_blank => ''}) %>

(which is magnificently SIMPLE, and I don't want to convolute it here, which is why I am asking the experts on this list...)

To NOT display records where Associate.deleted != 0 ?

Additionally, how would I amend it if I have and Associate.id that I want to be selected in it? Thanks guys.

How do I amend the form helper:

<%= select("lead", "associate_id", Associate.all.collect {|p| [ p.to_s, p.id ] }, {:include_blank => ''}) %>

(which is magnificently SIMPLE, and I don't want to convolute it here, which is why I am asking the experts on this list...)

To NOT display records where Associate.deleted != 0 ?

You could provide a named scope of Associate that finds records where deleted != 0 (called active or whatever is right for you) then use Associate.active instead of Associate.all. In fact I would also provide a method of Associate that returns the collected data, then it becomes even simpler <%= select("lead", "associate_id", Associate.options_for_select, {:include_blank => ''}) %> This also makes testing simpler as you can test options_for_select in a unit test.

Additionally, how would I amend it if I have and Associate.id that I want to be selected in it? Thanks guys.

Sorry I don't understand the question

Colin

Suppose I have the following then in my view -- I am creating a form for the model 'Lead' and there is a field lead.associate_id. In my controller, I am setting a variable to a model @Up, and @Up has an associate_id also. I would like the default lead.associate_id be set in my select with the @Up.associate_id. Please note the second line in my view code below (which does not work, of course). How can I default the lead.associate_id on this form to create a new lead to be equal to the @Up.associate_id ?

<% form_for :leads, :url => { :controller => 'channels', :action => "lead" }, :html => {:name => 'leadform'} do |f| %> <% lead.associate_id = @Up.associate_id %> <%= select("lead", "associate_id", Associate.find(:all, :order => 'associates.lastname ASC', :conditions => ['deleted=0']).collect {|p| [ p.to_s, p.id ]}, {:include_blank => ''}) %>

Suppose I have the following then in my view -- I am creating a form for the model 'Lead' and there is a field lead.associate_id. In my controller, I am setting a variable to a model @Up, and @Up has an associate_id also. I would like the default lead.associate_id be set in my select with the @Up.associate_id. Please note the second line in my view code below (which does not work, of course). How can I default the lead.associate_id on this form to create a new lead to be equal to the @Up.associate_id ?

<% form_for :leads, :url => { :controller => 'channels', :action =>

I would expect this to be :lead (singular)

"lead" }, :html => {:name => 'leadform'} do |f| %> <% lead.associate_id = @Up.associate_id %>

Should this be @lead.associate_id = @Up.associate_id?

<%= select("lead", "associate_id", Associate.find(:all, :order => 'associates.lastname ASC', :conditions => ['deleted=0']).collect {|p| [ p.to_s, p.id ]}, {:include_blank => ''}) %>

If you are using form_for why are you not using f.select?

Colin

Whoops! You're right!

<% form_for :lead, :url => { :controller => 'channels', :action => "lead" }, :html => {:name => 'leadform'} do |f| %> <%= f.label :origin %><br /> <%= f.select( "associate_id", Associate.find(:all, :order => 'associates.lastname ASC', :conditions => ['deleted=0']).collect {|p| [ p.to_s, p.id ]}, {:include_blank => ''}) %>

However, if I add in :

<% @lead.associate_id = @Up.associate_id %>

Nothing happens ? Additionally, I try making this statement in te method of the controller which renders the partial that this view code is in, again, nothing happens (i.e. it does not select the associate in the select box)

I use :associate_id in f.select but I don't think that should make any difference. Have you verified just before the f.select that @lead.associate_id is a value from the set? Display the id on the form and check the html (View, Page Source or similar in browser) to check that the id is in the selection set. Try it without the include_blank option (clutching at straws here).

Colin

I see where the problem is arising (but not its solution). My form is in a div that is hidden. My controller method is called via a javascript function, which also invoked the form (which is in a partial, in a div, re-rendering the partial) and the javascript, after calling the controller method, invokes a redbox on the div/partial with the form. This is causing a big problem here.

I must set the selected value for the select box in my javasript method I believe, before invoking redbox. I will post this to the appropriate javascript forum. Thanks for all of yoiur help on this knotty problem! -RVince