【发布时间】:2016-10-26 11:52:48
【问题描述】:
我对 RoR 还很陌生,目前正在为业余体育俱乐部经理开发一款免费的小应用程序,帮助他们跟踪付款。此应用程序显示相关的玩家数据和收取费用 (tarifa) 付款的表格。我想显示在输入付款之前支付的概念费用。这是带有表单和生成的 HTML 的应用程序视图:form for pagos
这是 pagos_controller.rb 'new' 操作:
def new
@pago = Pago.new
@conceptos = Concepto.all
@jugador = Jugador.find_by(id: params[:jugador_id])
end
这是渲染到views/pagos/new.html.erb中的views/pagos/_form.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 %>
这是 pagos 架构...
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
还有 Conceptos 架构:
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
我需要获取在“collection_select”中选择的值,以使用它来查找该概念的 tarifa,就像在
中一样@conceptos.find_by(name: theConceptoName).tarifa
并将其放入
$("#la-tarifa").text(tarifa)
显示值,但不知道如何从
中获取“theConceptoName”变量<%= f.collection_select :concepto, @conceptos, :name, :name %>
我尝试了几种方法,我想出了 jQuery 方法来获取值,所以它会是这样的:
$("#pago_concepto").change( function(){
var miConcepto = $("#pago_concepto").value();
do the @conceptos search for miConcepto and get the tarifa;(*)
$("#la-tarifa").text(tarifa);
});
但是我如何在 pagos.cofee 代码中执行查询 (*) 以从 @conceptos 获取 tarifa 值...我已经做了很多谷歌研究,但我发现的每个答案都让我更加困惑。 请帮帮我。
【问题讨论】:
标签: jquery ruby-on-rails ruby ruby-on-rails-3