Collection_select with two columns and a prompt

Hello,
I am quite a newbie to RoR, I hope you maybe able to help.

I have a collection_select in a _form.rhtml:

“collection_select(:assets, :model_id, @models, :id,
:model_ref,options={:prompt => ‘- Select a model -’})”

As you see my drop down menu will show the model_ref column.

  1. Is it possible to have more than one column. For example I would
    like to concatenate the “description” column after each model_ref in
    the drop down menu.
  2. The “options={:prompt” do not give back any feedback to use. The
    drop down list do not give prompt.
    Is it obsolete ?

Thanks in advance.

Best

Rhaps wrote:

like to concatenate the “description” column after each model_ref in
the drop down menu.

Yes. Create an instance method in your Model class like

def model_ref_and_description
model_ref + ’ ’ + description
end

and use this as the text method in your collection_select call.

  1. The “options={:prompt” do not give back any feedback to use. The
    drop down list do not give prompt.
    Is it obsolete ?

No, when you see a parameter like “options = {}” in the rdoc API
of a method, this is just showing you how the parameter is written
in the definition of the method in the core code. “options” is the
internal name this code uses to work with this parameter; the parameter
you send in is just a hash, defaulting to the empty hash when not
present.

So in your call above delete the characters “options=”, and just
send in the hash containing the :prompt option.


We develop, watch us RoR, in numbers too big to ignore.

Rhaps wrote:

“collection_select(:assets, :model_id, @models, :id,
:model_ref,options={:prompt => ‘- Select a model -’})”

2) The “options={:prompt” do not give back any feedback to use. The
drop down list do not give prompt.
Is it obsolete ?

But the real reason you’re not seeing the prompt is that it’s only
included and shown if the value is blank, which means you have
to define the model_id field in the assets table as default NULL.


We develop, watch us RoR, in numbers too big to ignore.

Rhaps wrote:

But I get an :
undefined method `model_ref_and_description’

You say you put it in your “models controller”. It should go
inside class Model, a class descended from ActiveRecord::Base.


We develop, watch us RoR, in numbers too big to ignore.

Mark,
Thanks a lot for the full explanation.
I’ve placed

def model_ref_and_description
model_ref + ’ ’ + description
end

into my models controller, and changed the collection_select to this :
<%= collection_select(:assets, :model_id, @models, :id,
:model_ref_and_description ) %>

But I get an :
undefined method `model_ref_and_description’

I may have forget something…

Oupsss sorry
I should stop working when I try to code at the same time.

Thanks a lot it rocks