Rails:在类似电子商务的环境中构建租赁订单和订单表

Rails: Structuring rental orders & order form in ecommerce-like setting

我想知道是否有人可以帮助我开发具有某些电子商务特征的应用程序。

上下文: 通过应用程序,一家自行车连锁店 ('chains') 可以出租

最后的注释:

错误信息: 不允许的参数::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