Arrays and choose one

Hi!!

I am newbie on ruby and I have questions and troubles, can you help me
out?

This is the html and the view


<select name="select" multiple size="3">
<option value="audi">Audi</option>
<option value="bmw">BMW</option>
<option value="dodge">Dodge</option>
<option value="nissan">Nissan</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:


  def type
      @audi=Array.new
      @audi=['audi 1','audi 2','audi 3','audi 4']

      @bmw=Array.new
      @bmw=['bmw 1','bmw 2','bmw 3','bmw 4']

      @dodge=Array.new
      @dodge=['dodge 1','dodge 2','dodge 3','dodge 4']

      @nissan=Array.new
      @nissan=['nissan 1','nissan 2','nissan 3','nissan 4']

      @renault=Array.new
      @renault=['renault 1','renault 2','renault 3','renault 4']

      @type=params[:select]
        option=@type
        case option
        when audi
          puts @audi
        when bmw
          puts @bmw
        when dodge
          puts @dodge
        when nissan
          puts @nissan
        when renault
          puts @renault
        end
  end

This code is bad?, good?, something are missing.?
thanks for the help

On Wed, Sep 21, 2011 at 14:22, Alen A. [email protected] wrote:

Audi

Renault

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

Thanks both of you!!

Let me check what I can do.

As the beginning way I did this:

The view (I have to choose one)

Audi BMW Dodge Nissan Renault =============================================

The Controller

def method

  @audi=['audi 1','audi 2','audi 3','audi 4']

  @bmw=['bmw 1','bmw 2','bmw 3','bmw 4']

  @dodge=['dodge 1','dodge 2','dodge 3','dodge 4']

  @nissan=['nissan 1','nissan 2','nissan 3','nissan 4']

  @renault=['renault 1','renault 2','renault 3','renault 4']

  @submarca=Array.new()
  case params[:select]
  when "audi"
    @submarca= @audi
  when "bmw"
    @submarca=@bmw
  when "dodge"
    @submarca= @dodge
  when "nissan"
    @submarca=@nissan
  when "renault"
    @submarca= @renault
  end
  puts "submarca" + @submarca.to_s

end

this works when I choose “audi” and the console (cmd) show me only the
array of audi.

Now the problem, I want the array [‘audi 1’,‘audi 2’,‘audi 3’,‘audi 4’]
show on the same page (view) as a drop list

So Id did this

same page of view

<%unless @anothermethod.nil?%>

The cars:

<% @submarca.each do |submarca| %> <%= submarca %> <%end%> ==================================== But its not working?!

Can you help me out?
thanks

Hai dude

I will give one ideal try it your self

// already you have array list in your controller file

@first_list_box = [1,10,3,4]
@second_list_box = [a,b,c,d]

//Here you have two choice
//one is combined two array…

@first_list_box.each do |first|
//here 0 is index, All is display, if we select “All"then value is
0
//so before get length of the array
index_num = @second_list_box.length
index_num = index_num + 1
@second_list_box.insert(index_num,[”#{first}",0)
end

// The result we be like this formet
@second_list_box = [a,b,c,d,1,2,3,4]

// so now you can use this way

//Second is only selected value is insert

//view or html.erb

<%= select(:table_name, :field_num1, @first_list_box,{:selected =>
session[:second_array]}) %>

//here session[:second_array] is like as temp variable so now i save
selected value in session[:second_array] variable if i select 1 then the
selected value is 1 that value i pass to session[:second_array] variable
so now

<% index_num = @second_list_box.length
index_num = index_num + 1
@second_list_box.insert(index_num,[“#{session[:second_array]}”,0)
%>

<%= select(:table_name, :field_num2, @second_list_box) %>

//now the result

if you select @first_array_list_box => 1 then you can get
@second_array_list_box values are like this [a,b,c,d,1]

if you select @first_array_list_box => 2 then you can get
@second_array_list_box values are like this [a,b,c,d,2]

if you select @first_array_list_box => 1 first time and second time 2
then you can get @second_array_list_box values are like this
[a,b,c,d,1,2]

this like is used for array handling
http://corelib.rubyonrails.org/classes/Array.html#M000431

bye :slight_smile: