Rails and shapes: drop down with a range of numbers and unlimited

I have it right now:

<%= f.select :credit, (0..500) %>

This will result in the following:

<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
...

How do I add another parameter in this selection, which will be " Everything ", and what value should be nil ?

+5
source share
2 answers

This will almost do what you want:

<%= f.select :credit, ((0..500).map {|i| [i,i] } << ["No limit",nil]) %>

selectcan accept several formats for the parameter list. One of them is an array of arrays, as indicated here. Each element in the external array is a 2-element array containing the displayed option text and the form value in that order.

map (0..500) , . .

, "( ) , " " - , - form, nil . , - javascript, , , :

param[:credit] == "" and param[:credit] = nil

.

+8

, options_for_select prompt, , , :

<%= f.select :credit, options_for_select(0..500), { prompt: "No Limit" } %>

: http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/select

+6

All Articles