1
I have a model Usuario
and for him I have two constants that define a type for him. To pass this information to a select
I have a method types that returns a Array
. Ex:
#models/Usuario.rb
class Usuario < ActiveRecord::Base
def tipos
[["Tipo 1",1], ["Tipo 2",2]]
end
end
#views/usuarios/_form.html.erb
<div class="field">
<%= f.label :tipo %><br>
<%= f.select :tipo,@usuario.tipos, :class=>"form-control" %>
</div>
So in mine index
will be displayed 1 and 2 respectively to their types. Obviously would like Type 1 and Type 2 to be displayed in each case.
Currently as I’m doing so:
#models/Usuario.rb
class Usuario < ActiveRecord::Base
def tipos
[["Tipo 1",1], ["Tipo 2",2]]
end
def tipo_tos
#inverte chave, valor, converte em Hash, pega o nome do tipo
Hash[tipos.map { |t| [t[1],t[0]] }][self.tipo]
end
end
Some more efficient way to do this?
Very good @Guilherme !
– Luiz Carvalho