我真的一直在摸不着头脑,非常感谢帮助。我有一个商店设置,人们可以在那里上课。我有课程模型、订单模型和优惠券模型。以下是模型中的关联
class Course < ActiveRecord::Base
belongs_to :category
has_many :orders
has_many :coupons
end
class Order < ActiveRecord::Base
belongs_to :course
belongs_to :user
belongs_to :coupon
end
class Coupon < ActiveRecord::Base
belongs_to :course
has_many :orders
end
我有一个非常简单的优惠券模型设置,其中包含 code 和 newprice 列。我希望某人能够在新订单页面上填写优惠券表格并更新价格。
在我看来,对于新订单,我有两种形式,一种用于新订单,另一种用于优惠券。如果用户输入了正确的优惠券代码,如何签入我的控制器?如何更新要显示的优惠券价格而不是课程价格?
这是我的订单控制器
class OrdersController < ApplicationController
before_action :set_order, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
def index
@orders = Order.all
end
def show
end
def new
course = Course.find(params[:course_id])
@course = Course.find(params[:course_id])
@order = course.orders.build
@coupon = Coupon.new
@user = current_user.id
@useremail = current_user.email
end
def discount
course = Course.find(params[:course_id])
@order = course.orders.build
@user = current_user.id
@useremail = current_user.email
end
def edit
end
def create
@order = current_user.orders.build(order_params)
if current_user.stripe_customer_id.present?
if @order.pay_with_current_card
redirect_to @order.course, notice: 'You have successfully purchased the course'
else
render action: 'new'
end
else
if @order.save_with_payment
redirect_to @order.course, notice: 'You have successfully purchased the course'
else
render action: 'new'
end
end
end
def update
if @order.update(order_params)
redirect_to @order, notice: 'Order was successfully updated.'
else
render action: 'edit'
end
end
def destroy
@order.destroy
redirect_to orders_url
end
private
# Use callbacks to share common setup or constraints between actions.
def set_order
@order = Order.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def order_params
params.require(:order).permit(:course_id, :user_id, :stripe_card_token, :email)
end
end