Topic: Show and update field on checkbox
I'm rather new to rails and especially with ajax, but I'm trying to learn.
I've been looking around but I can't find a straightforward way to do this, although I have the feeling it's something very basic, and that I should have figured this out already by now
I have a Bills model.
In the view for the 'new' action, there's a text_field for 'amount', and a check_box for 'vat_applicable'.
If the checkbox is checked, I'd like the :onchange to display a second text_field, for 'vat', which automatically sets the value to the value of the amount field, times 0.21.
I manage to get the checkbox to be displayed, but
1. I'd like it to be hidden when the page loads (it's displayed by default), and
2. I can't figure out how to set the calculated value for 'vat'. I'd like to do this with an action from the controller, but how?
First post, I hope I haven't violated too many rules
<div id="amount" class="field"> <%= f.label :amount %><br /> <%= f.text_field :amount, :size => 5 %> € </div> <div class="field"> <%= f.label :vat_applicable %><br /> <%= f.check_box :vat_applicable, :onclick => "document.getElementById('vat').style.display = (this.checked ? 'inline' : 'none');" %> </div> <div id="vat" class="field"> <%= f.label :vat %><br /> <%= f.text_field :vat, :size => 5 %> € </div>