2010-04-20 6 views
1

j'ai créé un bouton de sélection avec option 3comment changer la valeur d'un nom selon le cas

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

La valeur est insérée à la base, mais quand je l'afficher i voir le numéro op l'option sélectionné (ce qui est correct).

Ce que je voudrais savoir comment je peux changer cela si l'indice l'utilisateur peut voir le nom et non la valeur:

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

fin

Cela n'a pas fonctionné. Regars

Répondre

2

il sera plus facile avec un hachage, 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 
+0

Cette option a fonctionné exactement comme je l'ai besoin, je vous remercie beaucoup. – ZeroSoul13

+0

pas de problème =) content que ça a aidé – Staelen

2

Remplacer la méthode prioridad dans votre modèle comme suit:

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

Maintenant, la vue affichera des valeurs de chaîne pour la prioridad.

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

p.prioridad = 5 
p.prioridad #nil 

p.prioridad = 3 
p.prioridad #Baja 
Questions connexes