我正在尝试通过名为orders_products的连接表将订单和产品链接在一起,订单有很多:产品虽然:order_products和产品有很多:订单虽然:order_products,但是当我保存表单并查看时视图没有为该订单显示产品。
new.html.erb for Order
<%= simple_form_for @order do |f| %>
<%= f.input :order_number %>
<%= hidden_field_tag "order[product][]", nil %>
<% Product.all.each do |product| %>
<%= check_box_tag "order[product_ids][]", product.id, @order.product_ids.include?(product.id), id: dom_id(product) %>
<%= label_tag dom_id(product), product.name %><br>
<% end %>
<%= f.submit %>
order.rb
class Order < ActiveRecord::Base
belongs_to :customer
has_many :order_products, class_name: "OrderProduct"
has_many :products, through: :order_products
end
product.rb
class Product < ActiveRecord::Base
has_many :order_products, class_name: "OrderProduct"
has_many :orders, through: :order_products
end
order_product.rb
class OrderProduct < ActiveRecord::Base
belongs_to :product
belongs_to :order
end
show.html.erb for Order
<div class="well">
<%= "Order number " + @order.order_number %>
<% @order.products.each do |product| %>
<%= product.name %>
<% end %>
</div>