Create a collection select from a array of numbers 1-50

@make_num = (1…50).entries

something like?

<%= f.collection_select(:make_num, @make_num.collect {|c| [c, c]}) %>

i get wrong number of arguments (2 for 4)

thanks!

<%= f.select “make_num”, 1…50 %>

On Thu, May 22, 2008 at 3:00 PM, Scott K. <
[email protected]> wrote:


Posted via http://www.ruby-forum.com/.


Appreciated my help?
Recommend me on Working With Rails
http://workingwithrails.com/person/11030-ryan-bigg

Ryan B. wrote:

<%= f.select “make_num”, 1…50 %>

great thanks!! sometimes things are just too easy.

I think that’d be something like:

<%= f.select(:level, 1…200, {:prompt=> “N/A”}) %>

(Not sure if those curly braces are necessary, but they shouldn’t
hurt…)

what if i want to add a default value of null at the top if that field
is not applicable (with a label of N/A)?

something like:

<%= f.select(:level, 1…200 + “N/A”) %>

Scott K. wrote:

Roy P. wrote:

I think that’d be something like:

<%= f.select(:level, 1…200, {:prompt=> “N/A”}) %>

(Not sure if those curly braces are necessary, but they shouldn’t
hurt…)

cool thanks!

oops one more thing. what if i want the option to be available when
editing the item too?

Roy P. wrote:

I think that’d be something like:

<%= f.select(:level, 1…200, {:prompt=> “N/A”}) %>

(Not sure if those curly braces are necessary, but they shouldn’t
hurt…)

cool thanks!

Hmmm–that’s different I think. With :prompt, the string you give isn’t
actually stored in the db–it’ll get translated to a null. You can use
the very same call in your edit view, and again nulls should be
translated into the prompt value. But if you really want to store “N/A”
in the db, you’ll have to do something like:

In your model, say:

class MyModel < ActiveRecord::Base
ALLOWABLE_LEVELS = (1…200).to_a << “N/A”
end

and then:

<%= f.select(:level, MyModel::ALLOWABLE_LEVELS) %>

Mind you, your level field will have to accept strings if you do this…

HTH,

-Roy

Roy P. wrote:

Hmmm–that’s different I think. With :prompt, the string you give isn’t
actually stored in the db–it’ll get translated to a null. You can use
the very same call in your edit view, and again nulls should be
translated into the prompt value. But if you really want to store “N/A”
in the db, you’ll have to do something like:

In your model, say:

class MyModel < ActiveRecord::Base
ALLOWABLE_LEVELS = (1…200).to_a << “N/A”
end

and then:

<%= f.select(:level, MyModel::ALLOWABLE_LEVELS) %>

Mind you, your level field will have to accept strings if you do this…

HTH,

-Roy

thanks again roy that helps. the only thing is that edit shows the
prompt N/A only if the value is already null.

if i set the level to 3 and then edit. there is no more N/A to chose
from.

but yea i would want the value to be stored as NULL when selecting N/A.