Ruby on Rails 和优惠券模型

2023-12-04

我真的一直对此摸不着头脑,非常感谢您的帮助。我有一个商店,人们可以在那里学习课程。我有课程模型、订单模型和优惠券模型。这是模型中的关联

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

我有一个非常简单的优惠券模型设置,其中包含代码和新价格列。我希望有人能够在新订单页面上填写优惠券表格并更新价格。

在我看来,对于新订单,我有两种表格,一种用于新订单,另一种用于优惠券。如果用户输入了正确的优惠券代码,如何检查我的控制器?如何更新要显示的优惠券价格而不是课程价格?

这是我的订单控制器

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

您可以使用 AJAX 请求来完成此操作form_for帮助者与:remote option.

概括

  1. Set :remote选项true为您coupons表单提交 AJAX 请求。
  2. 创建控制器操作来处理来自表单的 AJAX 请求。
  3. 使用 JavaScript 响应控制器操作来更新您的orders表格(您认为的另一种表格)以及新的价格信息等。

使用 `:remote` 的 AJAX 请求

这是一些代表您的示例代码coupon form :

<%= form_for @coupon, method: :post,  url: check_coupon_code_path, remote: true do |f| %>
    <%= f.text_field :coupon_code, :placeholder => "Enter your coupon" %>
    <%= f.submit "Submit Coupon Code" %>
<% end %> 

请注意以下事项:

  • The :remote选项为form_for标签设置为true.
  • The :url选项是您的控制器操作的路径CouponsController。因为:remote选项设置为true,请求将发布到此:url选项作为 AJAX 请求。
  • In this code example, it's assuming it has a route defined like this in the routes.rb file to handle the AJAX request for checking the coupon code:
    • post 'check_coupon_code' => 'coupons#check_coupon_code'
    • 注:在forms_for帮手,那个:url选项附加_path到中定义的前缀routes.rb file.
    • 附注:使用命令rake routes查看可用路线及其各自的控制器操作目标。

在控制器中处理 AJAX 请求

In your CouponsController,定义动作check_coupon_code处理上面的 AJAX 请求form_for:

def check_coupon_code
  # logic to check for coupon code here

  respond_to do |format|
    if # coupon code is valid
      format.js   {}
    else
      # some error here
    end
  end
end

注意format.js in the respond_to动作的块。这允许控制器使用 JavaScript 响应 AJAX 请求来更新您的orders形式在你看来。你必须定义一个相应的app/views/coupons/check_coupon_code.js.erb查看生成将在客户端发送和执行的实际 JavaScript 代码的文件(或将 JavaScript 文件命名为check_coupon_code.js.coffee如果您使用的是 CoffeeScript)。

使用 JavaScript 更新

你的 JavaScriptcheck_coupon_code.js.erb文件将更新您的价格order form.

WARNING:即使您使用 JavaScript 在客户端(即浏览器)更改订单价格,在后端(即在您的控制器中)再次验证实际价格也至关重要,以防某些恶意用户试图操纵浏览器的请求等

你可以查看官方的 RailsGuide 来了解另一个example.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Ruby on Rails 和优惠券模型 的相关文章

随机推荐