Rails:在类似电子商务的环境中构建租赁订单和订单表
Rails: Structuring rental orders & order form in ecommerce-like setting
我想知道是否有人可以帮助我开发具有某些电子商务特征的应用程序。
上下文: 通过应用程序,一家自行车连锁店 ('chains') 可以出租
- 自行车('自行车'),
- 通过选择自行车类型,例如山地自行车、城市自行车等 ('bike_types) 和
- 自行车选项,例如头盔等 ('bike_options')
- 这取决于各个自行车商店 ('bike_stores')
- 此自行车租赁和选项将全部记录在一个订单中 ('orders')
- 订单和自行车之间的关系是多对多的,因此我创建了一个table来桥接这个('order_bikes')
最后的注释:
- 在租赁流程之前,连锁店所有者首先创建了 his/her (i) bike_stores、(ii) bike_types、(iii) 自行车和 (iv ) bike_options,应用程序的这一部分正在运行。因此,he/she只需要selectbike_types/bikes/options出先前创建的现有库存。
- 我通过省略 bike_options 来限制问题的范围,这主要是为了提供一些上下文以便理解数据库模式构建。
错误信息: 不允许的参数::bike_id
代码:
型号
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: true
end
class OrderBike < ApplicationRecord
belongs_to :bike
belongs_to :order
accepts_nested_attributes_for :bike
end
class Bike < ApplicationRecord
belongs_to :bike_type
validates :name, presence: true
has_many :order_bikes
has_many :orders, through: :order_bikes
end
class 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: true
end
class BikeStore < ApplicationRecord
has_many :bike_types, dependent: :destroy
has_many :orders, dependent: :destroy
end
订单控制器
class OrdersController < ApplicationController
def new
@bike_store = BikeStore.find(params[:bike_store_id])
@order = Order.new
@order.order_bikes.build
@bike_type_list = @bike_store.bike_types
end
def create
@order = Order.new(order_params)
@bike_store = BikeStore.find(params[:bike_store_id])
@order.bike_store = @bike_store
@order.save
redirect_to root_path
end
private
def order_params
params.require(:order).permit(:arrival, :departure,
order_bikes_attributes: [:id, :bike_quantity, :_destroy,
bikes_attributes: [:id, :name,
bike_types_attributes: [:id, :name]]])
end
end
查看
<%= simple_form_for [@bike_store, @order] do |f|%>
<%= f.simple_fields_for :order_bikes do |order_bike| %>
<%= order_bike.input :bike_quantity %>
<%= order_bike.association :bike %>
<% end %>
<%= f.input :arrival %>
<%= f.input :departure %>
<%= f.submit %>
<% end %>
如果您从简单形式 here 检查男女同校,您将看到方法关联的实际作用。
def association(association, options = {}, &block)
# ... simple form code here ...
attribute = build_association_attribute(reflection, association, options)
input(attribute, options.merge(reflection: reflection))
end
我们对 build_association_attribute
方法调用感兴趣。 here
def build_association_attribute(reflection, association, options)
case reflection.macro
when :belongs_to
(reflection.respond_to?(:options) && reflection.options[:foreign_key]) || :"#{reflection.name}_id"
# ... the rest of code ...
end
end
您订购的自行车型号有 belongs_to :bike
关联。因此,当您调用 order_bike.association :bike
时,它会在您的表单中构建 :bike_id
属性。如果您检查控制器的 params
散列,我相信您会从视图中看到该属性。
我在允许的参数中添加了 bike_id
。我希望它能解决你的问题..
def order_params
params.require(:order).permit(:arrival, :departure,
order_bikes_attributes: [:id, :bike_id, :bike_quantity, :_destroy,
bikes_attributes: [:id, :name,
bike_types_attributes: [:id, :name]]])
end
我想知道是否有人可以帮助我开发具有某些电子商务特征的应用程序。
上下文: 通过应用程序,一家自行车连锁店 ('chains') 可以出租
- 自行车('自行车'),
- 通过选择自行车类型,例如山地自行车、城市自行车等 ('bike_types) 和
- 自行车选项,例如头盔等 ('bike_options')
- 这取决于各个自行车商店 ('bike_stores')
- 此自行车租赁和选项将全部记录在一个订单中 ('orders')
- 订单和自行车之间的关系是多对多的,因此我创建了一个table来桥接这个('order_bikes')
最后的注释:
- 在租赁流程之前,连锁店所有者首先创建了 his/her (i) bike_stores、(ii) bike_types、(iii) 自行车和 (iv ) bike_options,应用程序的这一部分正在运行。因此,he/she只需要selectbike_types/bikes/options出先前创建的现有库存。
- 我通过省略 bike_options 来限制问题的范围,这主要是为了提供一些上下文以便理解数据库模式构建。
错误信息: 不允许的参数::bike_id
代码:
型号
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: true
end
class OrderBike < ApplicationRecord
belongs_to :bike
belongs_to :order
accepts_nested_attributes_for :bike
end
class Bike < ApplicationRecord
belongs_to :bike_type
validates :name, presence: true
has_many :order_bikes
has_many :orders, through: :order_bikes
end
class 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: true
end
class BikeStore < ApplicationRecord
has_many :bike_types, dependent: :destroy
has_many :orders, dependent: :destroy
end
订单控制器
class OrdersController < ApplicationController
def new
@bike_store = BikeStore.find(params[:bike_store_id])
@order = Order.new
@order.order_bikes.build
@bike_type_list = @bike_store.bike_types
end
def create
@order = Order.new(order_params)
@bike_store = BikeStore.find(params[:bike_store_id])
@order.bike_store = @bike_store
@order.save
redirect_to root_path
end
private
def order_params
params.require(:order).permit(:arrival, :departure,
order_bikes_attributes: [:id, :bike_quantity, :_destroy,
bikes_attributes: [:id, :name,
bike_types_attributes: [:id, :name]]])
end
end
查看
<%= simple_form_for [@bike_store, @order] do |f|%>
<%= f.simple_fields_for :order_bikes do |order_bike| %>
<%= order_bike.input :bike_quantity %>
<%= order_bike.association :bike %>
<% end %>
<%= f.input :arrival %>
<%= f.input :departure %>
<%= f.submit %>
<% end %>
如果您从简单形式 here 检查男女同校,您将看到方法关联的实际作用。
def association(association, options = {}, &block)
# ... simple form code here ...
attribute = build_association_attribute(reflection, association, options)
input(attribute, options.merge(reflection: reflection))
end
我们对 build_association_attribute
方法调用感兴趣。 here
def build_association_attribute(reflection, association, options)
case reflection.macro
when :belongs_to
(reflection.respond_to?(:options) && reflection.options[:foreign_key]) || :"#{reflection.name}_id"
# ... the rest of code ...
end
end
您订购的自行车型号有 belongs_to :bike
关联。因此,当您调用 order_bike.association :bike
时,它会在您的表单中构建 :bike_id
属性。如果您检查控制器的 params
散列,我相信您会从视图中看到该属性。
我在允许的参数中添加了 bike_id
。我希望它能解决你的问题..
def order_params
params.require(:order).permit(:arrival, :departure,
order_bikes_attributes: [:id, :bike_id, :bike_quantity, :_destroy,
bikes_attributes: [:id, :name,
bike_types_attributes: [:id, :name]]])
end