Topic: Generate list of price ranges, then bind to select_tag?

Hey all,

I need to generate a select tag with the following:

under 50,000
100,000
... up in 100,000 increments
900,000
1,000,000 and over

and im totally stuck, I could just type it out myself directly into html, but that defeats the point.

I have this in my controller action:

@prices = { '50000' => 'under 50,000' }

as a starting point, then I was invisaging doing a loop, which looked like this (don't laugh)

@price = 100000
    10.times do
      @prices << { @price => @price }
      @price = @price + 100000
    end

Then the select tag as so:
<%= select_tag 'min_price', options_for_select(@prices.map {|key, value|}) %>

Currently the loop just kills everything, but with that gone I did expect to atleast get one entry in the select for the 50,000 under 50,000 option, but I get an emtpy option tag instead: <option value=""></option>

I think SOS is the appropriate term to use here wink

Re: Generate list of price ranges, then bind to select_tag?

The "map" block is returning nothing. Actually you can probably just pass the hash itself:

options_for_select(@prices)

Railscasts - Free Ruby on Rails Screencasts

Re: Generate list of price ranges, then bind to select_tag?

Thanks Ryan, I figured that bit out eventually.

I got the loop working by using @prices.merge!({'key' => 'value'}) but I then tried to use number_to_currency and that's not available in controllers.. which leads me to think this code should be in the view? but that doesn't sound right..

And then my next problem is that these hashe's seem to resort themselves randomly, rather than in insert order.

sad

Re: Generate list of price ranges, then bind to select_tag?

MatthewKing wrote:

I got the loop working by using @prices.merge!({'key' => 'value'}) but I then tried to use number_to_currency and that's not available in controllers.. which leads me to think this code should be in the view? but that doesn't sound right..

A helper method is probably the best choice.

MatthewKing wrote:

And then my next problem is that these hashe's seem to resort themselves randomly, rather than in insert order.

Hashes are orderless. If you need to keep order it's best to use an array instead.

If you need more help I can whip up some code.

Railscasts - Free Ruby on Rails Screencasts