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

时间:2019-09-25 09:48:19

标签: ruby-on-rails activerecord e-commerce

我想知道是否有人可以帮我解决具有电子商务特征的应用程序。

上下文:通过该应用,自行车商店链(“链条”)可以出租

  • 自行车('自行车'),
  • 通过选择诸如山地自行车,城市自行车等自行车类型(' bike_types )和
  • 自行车选项,例如头盔等(' bike_options ')
  • 取决于各个自行车商店(' bike_stores ')
  • 这辆自行车和选项的租赁将全部按照订单('订单')
  • 记录
  • 订单与自行车之间的关系是多对多的,因此我创建了一个表格来桥接此订单(' order_bikes ')

最后的笔记:

  • 在租赁过程之前,连锁店所有者首先创建了自己的(i)bike_stores,(ii)bike_types,(iii)bike和(iv)bike_options ,该应用程序的这一部分正在正常工作。因此,他/她只需要从先前创建的现有清单中选择bike_types / bikes / options。
  • 我通过省略bike_options来限制问题的范围,这主要是为了提供一些上下文,以便理解db模式的建立。

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

1 个答案:

答案 0 :(得分:2)

如果您从简单的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