【发布时间】:2023-03-10 08:25:01
【问题描述】:
我正在尝试构建一个发票应用程序,其中每个发票都有一个行发票项目(例如产品名称、成本、金额)。
我希望当我从发票项目的 下拉列表 中选择 productname 时,会触发一个 ajax 来查询 product 数据库表 与 params[:product_id],从数据库中获取产品 cost 并因此立即填充项目成本。
即当用户选择产品名称时,无需输入该产品成本就会立即显示出来。
在下面的附图中,我的 query_string 按预期返回 JSON,除了我不知道如何将返回的 data 传递/显示到 cost_field。
如果我执行 html(product.cost),我会在 #notification 中获取/返回 [object, object] 并在 undefind cost 中导轨控制台也是如此。
我的问题是,我该如何做这样的事情? $("#notification").html(product.cost)
型号:
class Invoice < ApplicationRecord
has_many :items, dependent: :destroy
accepts_nested_attributes_for :items, allow_destroy: true, reject_if: proc {|a| a[:product_id].blank?}
has_many :products, through: :item
end
class Item < ApplicationRecord
belongs_to :invoice, optional: true
belongs_to :item, optional: true
end
class Product < ApplicationRecord
has_many :items
end
控制器
class InvoicesController < ApplicationController
before_action :set_invoice, only: [:show, :edit, :update, :destroy]
def index
@invoices = Invoice.all
end
def new
@invoice = Invoice.new
2.times { @invoice.items.build }
end
def pricedata
@product = Product.select(:id, :cost, :productname).where('products.id = ?', params[:product_id])
respond_to do |format|
format.html { render html: @product.id }
format.js #{ render js: @product}
format.json { render json: @product }
end
private
def invoice_params
params.require(:invoice).permit(:name, :amount, items_attributes: [:qty, :price, :id, :product_id, :_destroy])
end
表格
<p id="notification"></p> # notice to keep my eyes on return obj
...
<%= f.fields_for :items do |item| %>
<span><%= item.label :product_id %> </span>
<span> // # collection begins here
<%= item.collection_select(:product_id, Product.all, :id, :productname,
{prompt: "Select a product"},
{class: 'selectors' multiple: true })
%>
</span> // # collection ends here
<span><%= item.label :qty %> </span>
<span><%= item.number_field :qty %> </span>
<span><%= item.label :price, class: "p_price" %> </span>
<span><%= item.number_field :price %> </span>
<span><%= item.check_box :_destroy %> Remove item</span>
...
JAVASCRIPT / 咖啡
$(document).ready ->
$('.selectors').on "change", ->
selectOptions= $('.selectors option:selected')
product_id = $(this).val()
if selectOptions.length > 0
selectOptions.each ->
$.ajax({
url: '/invoices/:id/pricedata?product_id=' + product_id,
type: 'GET',
dataType: 'html',
contentType: 'application/html'
success: (product)->
$("#noticification").html(product)
console.log(product)
})
【问题讨论】:
-
按照@max 的建议,我能够解决问题。我像这样直接向 ProductController 发送 ajax 请求..... $.ajax({ url: '/products/' + product_id, ..... $("#notification").html (product.cost) })
标签: javascript jquery ruby-on-rails ruby-on-rails-5