--
Você recebeu essa mensagem porquê está inscrito no Google
Groups "rails-br".
Para enviar uma mensagem para o grupo, mande um email para rail...@googlegroups.com
Para se descadastrar, mande um e-mail para
rails-br+u...@googlegroups.com
Visite o grupo em http://groups.google.com/group/rails-br?hl=pt-BR
Leia nossa política de uso: http://goo.gl/YGgt7
sprintf('%.2f', self)
Abraço --
--
Você recebeu essa mensagem porquê está inscrito no Google
Groups "rails-br".
Para enviar uma mensagem para o grupo, mande um email para rail...@googlegroups.com
Para se descadastrar, mande um e-mail para
rails-br+u...@googlegroups.com
Visite o grupo em http://groups.google.com/group/rails-br?hl=pt-BR
Leia nossa política de uso: http://goo.gl/YGgt7
---
Você está recebendo esta mensagem porque se inscreveu no grupo "rails-br" dos Grupos do Google.
Para cancelar a inscrição neste grupo e parar de receber seus e-mails, envie um e-mail para rails-br+u...@googlegroups.com.
Para obter mais opções, acesse https://groups.google.com/groups/opt_out.
<%=f.text_field :valor, :value => (to_real(@item.valor) unless @item.new_record?), :class => 'moeda' %>sendo que esse to_real era um método que eu tinha colocado no ApplicationHelperdef to_real(valor) number_to_currency(valor) ende esse :class => 'moeda', tem um jquery associado, q eu "peguei emprestado" do site do banco do brasil :D é meio grandinho mas no tempo funcionou legal no projeto$(document).ready(function() { function filtraCampoValor(valor){ var s = ""; var cp = ""; vr = valor; tam = vr.length; for (i = 0; i < tam ; i++) { if (vr.substring(i,i + 1) >= "0" && vr.substring(i,i + 1) <= "9"){ s = s + vr.substring(i,i + 1);} } valor = s; return cp = valor; } $(".moeda").css('text-align', 'right').val(function(){ $(this).val(filtraCampoValor($(this).val())); vr = $(this).val(); tam = vr.length; if ( tam == 1 ){ return '0,0' + vr; } if ( tam == 2 ){ return '0,' + vr; } if ( (tam > 2) && (tam <= 5) ){ return vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ); } if ( (tam >= 6) && (tam <= 8) ){ return vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); } if ( (tam >= 9) && (tam <= 11) ){ return vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); } if ( (tam >= 12) && (tam <= 14) ){ return vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); } if ( (tam >= 15) && (tam <= 18) ){ return vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam );} }); $(".moeda").keyup(function () { $(this).val(filtraCampoValor($(this).val())); vr = $(this).val(); tam = vr.length; if ( tam <= 2 ){ $(this).val(vr); } if ( (tam > 2) && (tam <= 5) ){ $(this).val(vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam )); } if ( (tam >= 6) && (tam <= 8) ){ $(this).val(vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam )); } if ( (tam >= 9) && (tam <= 11) ){ $(this).val(vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam )); } if ( (tam >= 12) && (tam <= 14) ){ $(this).val(vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam )); } if ( (tam >= 15) && (tam <= 18) ){ $(this).val(vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ));} }); });
vê se te ajuda
Galera vou postar os passos que fiz como teste. No meu _form produto alterei:
<div class="control-group">
<%= f.label :preco_compra, "Preço da compra: R$", :class => "control-label" %>
<div class="controls">
<%= f.text_field :preco_compra,
:class => "moeda",
:value => (to_real(@produto.preco_compra) unless @produto.new_record?) %>
</div>
</div>
No meu app>helpers>application_helper fiz:
def to_real(valor)
number_to_currency(valor)
end
E no meu app>assets>javascript>application.js - copiei e colei o código inteiro emprestado do amigo Alexandre Veigas, que orientou para essa solução.
E agora? Tenho o index.html.erb e o show.html.erb - Olhando o código não foi muito diferente do que fiz para a data. Criei um helper string_to_date, uma mascara em js e
nas telinhas eu ponho dt_nascimento.strftime("%d/%m/%Y") e pronto, lá se exibe a data correta/ editar.. Tudo certinho.
O que devo colocar em preco_compra.ALGUMA COISA
Como o nosso amigo falou, não exibir correto, não significa que esta armazenando errado. Então o meu problema é só na hora de exibir.var Helpers = (function() {var Helper = function() { }// transforma a float into a currency formatHelper.currency_format = function (value) {if (value == "0") return "R$ 0,00";if (typeof value == "string") value = parseFloat(value);var element = $("<p>");element.text(value.toFixed(2));return element.text();};// transforma a float into a currency formatHelper.currency_to_float = function (value) {value = value.replace("R$ ", "");for(var f in value) {if(value[f] == "."){value = value.replace(".", "");}if(value[f] == ","){value = value.replace(",", ".");}}value = parseFloat(value);return value;};