Ruby on rails Rails select拥有许多获得正确Id的功能
我试图通过select将产品的大小传递给订单。该产品有很多尺寸,但有很多。 原始页面上的size.id与我在产品页面上选择的size.id不匹配。 有人知道为什么吗 例如,如果在订单页面上选择product size.id(2),则显示size.id(4) 尺寸型号Ruby on rails Rails select拥有许多获得正确Id的功能,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 4,我试图通过select将产品的大小传递给订单。该产品有很多尺寸,但有很多。 原始页面上的size.id与我在产品页面上选择的size.id不匹配。 有人知道为什么吗 例如,如果在订单页面上选择product size.id(2),则显示size.id(4) 尺寸型号 attr_accessible :size has_many :orders has_many :sizeship has_many :products, :through => :sizeship has
attr_accessible :size
has_many :orders
has_many :sizeship
has_many :products, :through => :sizeship
has_many :orders
has_many :sizeships
has_many :sizes, through: :sizeships
belongs_to :cart
belongs_to :product, touch: true
belongs_to :size
belongs_to :user
has_many :orders
产品型号
attr_accessible :size
has_many :orders
has_many :sizeship
has_many :products, :through => :sizeship
has_many :orders
has_many :sizeships
has_many :sizes, through: :sizeships
belongs_to :cart
belongs_to :product, touch: true
belongs_to :size
belongs_to :user
has_many :orders
订单模式
attr_accessible :size
has_many :orders
has_many :sizeship
has_many :products, :through => :sizeship
has_many :orders
has_many :sizeships
has_many :sizes, through: :sizeships
belongs_to :cart
belongs_to :product, touch: true
belongs_to :size
belongs_to :user
has_many :orders
购物车型号
attr_accessible :size
has_many :orders
has_many :sizeship
has_many :products, :through => :sizeship
has_many :orders
has_many :sizeships
has_many :sizes, through: :sizeships
belongs_to :cart
belongs_to :product, touch: true
belongs_to :size
belongs_to :user
has_many :orders
产品控制器
def show
@product = Product.cached_find(params[:id])
@sizes_for_dropdown = @product.sizes.collect { |s| [s.size, s.id] }
end
def add
product = Product.find(params[:id])
if product
if product.buyable?(current_user)
current_user.cart = Cart.new #if current_user.cart.nil?
size = product.sizes.find_by_id(params[:size_id])
quantity = params[:quantity].to_i > 0 ? params[:quantity].to_i : 1
order = current_user.cart.orders.find_by_product_id_and_status_and_size_id(product.id, nil, product.sizes.nil? ? nil : product.sizes.find { |l| l.id } )
if order.nil? # create new order
order = Order.new
order.product = product
order.size = product.sizes.find { |k| k.id }
current_user.cart.orders << order
else # increase quantity
order.quantity += quantity
order.save
end
redirect_to product_path(product)
flash[:success] = "Success"
else
redirect_to product_path(product)
flash[:error] = " Error"
end
else
redirect_to :shop, flash[:error] = 'Error'
end
end
购物车控制器
def show
@product = Product.cached_find(params[:id])
@sizes_for_dropdown = @product.sizes.collect { |s| [s.size, s.id] }
end
def add
product = Product.find(params[:id])
if product
if product.buyable?(current_user)
current_user.cart = Cart.new #if current_user.cart.nil?
size = product.sizes.find_by_id(params[:size_id])
quantity = params[:quantity].to_i > 0 ? params[:quantity].to_i : 1
order = current_user.cart.orders.find_by_product_id_and_status_and_size_id(product.id, nil, product.sizes.nil? ? nil : product.sizes.find { |l| l.id } )
if order.nil? # create new order
order = Order.new
order.product = product
order.size = product.sizes.find { |k| k.id }
current_user.cart.orders << order
else # increase quantity
order.quantity += quantity
order.save
end
redirect_to product_path(product)
flash[:success] = "Success"
else
redirect_to product_path(product)
flash[:error] = " Error"
end
else
redirect_to :shop, flash[:error] = 'Error'
end
end
大家好,欢迎来到stack overflow。谢谢你提供了所有相关的代码,这真的很有帮助。我仍然有点不确定到底出了什么问题——你能举个例子说明你所说的“错误的一个”是什么意思吗?你能给我们展示一些示例数据,以及你期望看到的,但你看到的是什么吗?谢谢你的兴趣@TarynEast,我编辑了我的问题。我的意思是订单页面上显示的size.id与我在产品页面上选择的size.id不匹配您好-但这还不足以让我们继续。你能给我们一些样本数据吗?比如id为2的产品里面有什么,id为4的产品里面有什么?您如何选择它们?您使用什么代码选择它们?你得到的尺码是多少(给我们看看),你期望的尺码是多少(给我们看看,不要说)。另外:你提到“订单页面”-但没有告诉我们:)好的,我会发布一个屏幕!对,在屏幕上,如果我在订单上选择M,则显示GGHi并欢迎使用堆栈溢出。谢谢你提供了所有相关的代码,这真的很有帮助。我仍然有点不确定到底出了什么问题——你能举个例子说明你所说的“错误的一个”是什么意思吗?你能给我们展示一些示例数据,以及你期望看到的,但你看到的是什么吗?谢谢你的兴趣@TarynEast,我编辑了我的问题。我的意思是订单页面上显示的size.id与我在产品页面上选择的size.id不匹配您好-但这还不足以让我们继续。你能给我们一些样本数据吗?比如id为2的产品里面有什么,id为4的产品里面有什么?您如何选择它们?您使用什么代码选择它们?你得到的尺码是多少(给我们看看),你期望的尺码是多少(给我们看看,不要说)。另外:你提到“订单页面”-但没有告诉我们:)好的,我会发布一个屏幕!对,如果我在订单上选择M,屏幕上显示GG