in my rails 3 application, there is a select_tag with an onchange event, which sends an ajax request:
f.select 'forma_id', @collection, {:include_blank => true}, {:onchange => "goAjax('/lancamentos/receber_forma', this.value)", :class => 'btn'}
where, goAjax is:
function goAjax(_url, _value) {
$.ajax({url: _url, data: 'value=' + _value, dataType: 'script'})
}
lancamentos_controller#receber_forma is (simplified version):
def receber_forma
@lancamento = Lancamento.new(:forma_id => params[:value])
logger.info 'forma_id = ' + @lancamento.forma_id
respond_to do |format|
format.html { render :new }
format.js
end
end
and receber_forma.js.erb:
$('div#cartoes').replaceWith("<%= escape_javascript(label_tag(@lancamento.forma_id)) %>");
what is happening, is that when I choose a value on that select, the value rendered on the label is always the value sent on first call. The following value changes simply don’t change the label text. The logger in the method receber_forma shows that the new selected value is always sent.
Someone knows what I’m doing wrong here??
p.s.:sorry any errors of English
You’re completely replacing your
div#cartoeswith a label, so the second time the code runs, it can’t find adivwith thecartoesid. You probably want to use.htmlinstead of.replaceWith, so that the label becomes the content of thedivrather than replacing it altogether: