I am fairly new to RoR, currently working on a little free app for amateur sports clubs managers that help them to keep track of payments. This app shows relevant player data and a form to collect fees (tarifa) payments. I want to show the fee for the concept being paid before the payment is entered. This is the app view with the form and the HTML generated: form for pagos
this is the pagos_controller.rb 'new' action:
def new
@pago = Pago.new
@conceptos = Concepto.all
@jugador = Jugador.find_by(id: params[:jugador_id])
end
and this is the views/pagos/_form.html.erb rendered into the views/pagos/new.html.erb :
<%= form_for(@pago) do |f| %>
<% if @pago.errors.any? %>
<p> All the error showing logic is omitted here for simplicity </p>
<div class="field-tm">
<%= f.label :concepto %>
<%= f.collection_select :concepto, @conceptos, :name, :name %>
</div>
<div class="field-tm">
<%= f.label :tarifa %> <div id="la-tarifa"> </div>
</div>
<div class="field-tm">
<%= f.text_field :cantidad, :placeholder => "Cantidad a pagar" %>
</div>
<%= f.hidden_field :jugador_id, :value => params[:jugador_id] %>
<div class="actions">
<%= f.submit "Registrar Pago", class: "btn btn-default"%>
</div>
<% end %>
This is the pagos schema...
create_table "pagos", force: :cascade do |t|
t.string "concepto"
t.decimal "cantidad"
t.integer "jugador_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
And the Conceptos schema:
create_table "conceptos", force: :cascade do |t|
t.string "name"
t.decimal "tarifa"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
I need to get the value selected in the 'collection_select' to use it to find that concept's tarifa like in
@conceptos.find_by(name: theConceptoName).tarifa
and put it in the
$("#la-tarifa").text(tarifa)
to show the value, but don't know how to get a 'theConceptoName' variable that works from the
<%= f.collection_select :concepto, @conceptos, :name, :name %>
I've tried several ways and I came up with jQuery way to get the value, so it would be something like this:
$("#pago_concepto").change( function(){
var miConcepto = $("#pago_concepto").value();
do the @conceptos search for miConcepto and get the tarifa;(*)
$("#la-tarifa").text(tarifa);
});
but then how do I do the query(*) within the pagos.cofee code to get the tarifa value from @conceptos... I have done a lot of google research but every answer I find confuses me more. Please help me.
Aucun commentaire:
Enregistrer un commentaire