gpt4 book ai didi

ruby-on-rails - 如何计算 Ruby on Rails 中表单文本字段的默认值?

转载 作者:行者123 更新时间:2023-12-04 05:28:44 26 4
gpt4 key购买 nike

我有一个表格 payments像这样:

<%= f.label :invoice_id %>
<%= f.select(:invoice_id, current_user.outstanding_invoices_collection) %>

<%= f.label :amount %>
<%= f.text_field :amount %>

我想知道是否有办法填充 amount 的值以某种方式文本字段,例如与相关联的未结余额 invoice ?

在我的 Invoice模型有这个功能:
def balance
payments.map(&:amount).sum - total
end

如何做到这一点?

最佳答案

我假设您想根据从下拉列表中选择的发票来填充文本框。在那种情况下

这个想法是

  • 您需要对发票下拉列表进行 ajax 调用。
  • 该 ajax 响应应该更新文本框的值。

  • 对于 rails-3,我认为建议以不可靠的方式执行此操作。 Here is a link you can follow .开始玩它,同时我会尝试制作一些功能性的东西。希望能再次取得好成绩。

    您是否正在寻找如何仅填充值?

    更新:

    这是ajax部分
    #Application.js or any sutable js file
    $(function($) {
    $("#your_drop_down_id").change(function() {
    #Your the url to your controller action here
    $.ajax({url: '/get_amount',
    data: 'invoice_id=' + this.value,
    dataType: 'script'})
    });
    });
    #in get_amount Action
    invoice = Invoice.find(params[:invoice_id]) #Other appropriate logic to get the invoice
    @amount = invoice.balance
    #get_amount.js.erb
    $('#your_text_box_id').val('<%= @amount %>');
    #routes.rb
    #This part is written following the gist: https://gist.github.com/3889180 by @TinTin
    resources :payments do
    collection do
    get 'get_amount'
    end
    end

    如果有任何部分让您感到困惑,请告诉我。

    关于ruby-on-rails - 如何计算 Ruby on Rails 中表单文本字段的默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12883537/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com