我在 JS/Ajax 方面相對較新,我注意到如何根據以前插入的相同(簡單_)形式的數據呈現動態選項。背景:通過該應用程序,自行車連鎖店(“chains”)可以出租名稱為“自行車 1”或“黃色自行車”(“自行車”)的自行車,通過挑選自行車類型,例如山地自行車、城市自行車等。 ('bike_types) 和自行車選項,例如頭盔等('bike_options')這取決于單個自行車商店('bike_stores')自行車和選項的租賃將全部記錄在訂單中(“訂單”)訂單和自行車之間的關系是多對多的,因此 - 我創建了一個表來橋接這個('order_bikes')懸而未決的問題:我能夠通過 JS 獲得自行車類型 ID。我如何使用這個自行車類型的 id 以相同的形式顯示屬于該自行車類型的自行車?如前所述,不幸的是,我不太熟悉在 Rails 應用程序中使用 JS/Ajax,因此如果可以在應編寫建議代碼的位置添加文件路徑(例如 app/javascript/組件/order.js 等)最后說明:在租賃過程之前,鏈所有者首先創建了他/她的 (i) 自行車商店、(ii) 自行車類型、(iii) 自行車和 (iv) 自行車選項,這部分應用程序正在運行。因此,他/她只需要從之前創建的現有庫存中選擇自行車類型/自行車/選項。我通過省略自行車選項來限制問題的范圍,這主要是為了提供一些上下文以了解構建的數據庫模式。楷模class Order < ApplicationRecord belongs_to :bike_store has_many :bike_types, through: :bike_store has_many :order_bikes, inverse_of: :order, dependent: :destroy accepts_nested_attributes_for :order_bikes, allow_destroy: trueendclass OrderBike < ApplicationRecord belongs_to :bike belongs_to :order accepts_nested_attributes_for :bikeendclass Bike < ApplicationRecord belongs_to :bike_type validates :name, presence: true has_many :order_bikes has_many :orders, through: :order_bikesendclass BikeType < ApplicationRecord belongs_to :bike_store has_many :bikes, dependent: :destroy accepts_nested_attributes_for :bikes, allow_destroy: true has_many :bike_options, dependent: :destroy accepts_nested_attributes_for :bike_options, allow_destroy: true validates :name, :bike_count, presence: trueendclass BikeStore < ApplicationRecord has_many :bike_types, dependent: :destroy has_many :orders, dependent: :destroyend
以 simple_form 呈現動態選擇列表
智慧大石
2021-10-29 13:47:33