Easiest way to design a form, where displayed form options are dependent on input

▼魔方 西西 提交于 2020-04-18 07:12:06

问题


I tried setting up a waaaaaaay too complex order form (How to set up a order form with options belonging to a product_category)

Therefore, I am trying to take it slow and split up my order form, where:

  • first an product_category is selected and
  • if the product_category is available, the user can select the product and options belonging to the product_category.

Is there a way to do this with a multi-step form http://railscasts.com/episodes/217-multistep-forms? Or are there easier methods to create forms based on input?

Code

models

class Order < ApplicationRecord
  belongs_to :store
  belongs_to :product

  has_many :order_options, dependent: :destroy
  has_many :options, through: :order_options
  accepts_nested_attributes_for :order_options
end

class OrderOption < ApplicationRecord
  belongs_to :option
  belongs_to :order
  accepts_nested_attributes_for :option
end

class Option < ApplicationRecord
  belongs_to :product_category
  has_many :order_options, dependent: :destroy
  has_many :orders, through: :order_options
end

class ProductCategory < ApplicationRecord
  belongs_to :store
  has_many :products, dependent: :destroy
  accepts_nested_attributes_for :products, allow_destroy: true
  has_many :options, dependent: :destroy
  accepts_nested_attributes_for :options, allow_destroy: true
end

来源:https://stackoverflow.com/questions/58800066/easiest-way-to-design-a-form-where-displayed-form-options-are-dependent-on-inpu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!