On Wed, Sep 21, 2011 at 14:22, Alen Alen <lanaizen / hotmail.com> wrote:

> <select name="select" multiple size="3">
> <option value="audi">Audi</option>
...
> <option value="renault">Renault</option>
> </select>
...
> The idea is to choose only one and that one show another list box with
> some results, for example:
>
> I want an audi, I selected the audi and then another list box shows the
> types of audi: sedan, coupe,.....
>
> This the code I have:
>
> [code]
>
> def type
>   @audi=Array.new
>   @audi=['audi 1','audi 2','audi 3','audi 4']

First off, you don't need the Array.new part there.  The second line
is assigning it a fresh Array, filled with those literals.  (I assume
the "audi 1", "audi 2", etc. is where you would be putting sedan,
coupe, etc., right?)

>   @type=params[:select]
>    option=@type
>    case option
>    when audi
>     puts @audi
...
>    when renault
>     puts @renault
>    end
> end

Depending on literals like that should stop and make you think whether
you can convert it to something more data-driven, where you won't have
to tweak code in order to add more stuff.  In this case, you can store
the body styles for each make, in a hash.  So for instance, suppose
Audis come in sedans, coupes, and hatchbacks, while Dodges are sedans,
coupes, and pickups.  You could construct a hash that says, in part:

@types = { :audi => [ :sedan, :coupe, :hatchback ],
           :dodge => [ :sedan, :coupe, :pickup ],
           ...
         }

Most likely you'd have a model method like Car.body_styles_by_make
that would return this hash, or it could even be a class constant.
Access this in your controller, to load up the data for use in the
view.

Then, when your user decides on the value for the first select (the
make), you can use what he picked (converted to a symbol, or you could
store it under strings to begin with) to reach into the hash and pull
out values for a second select (the body style).

BTW, I'm assuming I'm interpreting your intent correctly, to use one
select's chosen value to construct another select's list of possible
values.  Somewhat like how many job boards use your choice of state to
construct the list of cities you can then select.  Right?

-Dave

-- 
LOOKING FOR WORK, preferably Ruby on Rails, in NoVa/DC; see main web site.
Main Web Site: davearonson.com
Programming Blog: codosaur.us
Excellence Blog: dare2xl.com