如何在 Rails 4.0 表单中显示所选记录的关联值
How to show an associated value from a selected record in Rails 4.0 form
我对 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/_form.html.erb 渲染到 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 %>
这是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 值...我已经做了很多 google 研究但每个答案我发现更让我困惑。
请帮助我。
试试这个,只是一个简单的 ajax 查找:
JS:
$("#pago_concepto").change( function(){
var miConcepto = $("#pago_concepto").value();
$.getJSON('/tarifa/' + miConcepto, function(data) {
$("#la-tarifa").text(data.tarifa);
});
});
pagos_controller:
def tarifa
tarifa = Concepto.find_by(name: params[:concept_name]).tarifa
respond_to do |format|
format.json { render json: {tarifa: tarifa} }
end
end
routes.rb:
get '/tarifa/:concept_name', to: 'pagos#tarifa'
如果你愿意,将 js 转换为 coffeescript 应该相对容易。
我对 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/_form.html.erb 渲染到 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 %>
这是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 值...我已经做了很多 google 研究但每个答案我发现更让我困惑。 请帮助我。
试试这个,只是一个简单的 ajax 查找:
JS:
$("#pago_concepto").change( function(){
var miConcepto = $("#pago_concepto").value();
$.getJSON('/tarifa/' + miConcepto, function(data) {
$("#la-tarifa").text(data.tarifa);
});
});
pagos_controller:
def tarifa
tarifa = Concepto.find_by(name: params[:concept_name]).tarifa
respond_to do |format|
format.json { render json: {tarifa: tarifa} }
end
end
routes.rb:
get '/tarifa/:concept_name', to: 'pagos#tarifa'
如果你愿意,将 js 转换为 coffeescript 应该相对容易。