views:

37

answers:

2

i've created a select button with 3 option

<%= f.label :prioridad %><br/>
<%= f.select :prioridad, options_for_select([['Alta', 1], ['Medio', 2], ['Baja', 3]]) %>

The value gets inserted to the DataBase but when i display it i see the number op the option selected (which is correct).

What i would like to know is how i can change that so on the index the user can see the name and not the value:

def convertidor
  case llamada.prioridad
    when prioridad == '1'
      puts "Alta"
    when prioridad == '2'
      puts "Media"
    else
     puts "Baja"
  end

end

This didn't worked. Regars

+2  A: 

Override the prioridad method in your model as follows:

class Model
  PRIORITIES = [nil, "Alta", "Media", "Baja"]
  def prioridad
    PRIORITIES[attributes['prioridad']||0]
  end
end

Now the view will display string values for the prioridad.

p.prioridad #nil
p.prioridad = 1
p.prioridad #Alta

p.prioridad = 5
p.prioridad #nil

p.prioridad = 3
p.prioridad #Baja
KandadaBoggu
A: 

it'll be easier with a hash, etc

  class Model < ActiveRecord::Base
    ...

    # note that self[:prioridad] will return the value from the database
    # and self.prioridad will call this method that is overriding the original method

    def prioridad
      hash = {1 => "Alta", 2 => "Media"}

      return "Baja" if hash[self[:prioridad]].nil?
      hash[self[:prioridad]]
    end

    ...
  end
Staelen
This option worked exactly as i needed, thank you so much.
ZeroSoul13
no prob =) glad it helped
Staelen