Ruby on rails 和优惠券模型 [英] Ruby on rails and coupon model

查看:59
本文介绍了Ruby on rails 和优惠券模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的一直在挠头,非常感谢帮助.我有一个商店设置,人们可以在那里参加课程.我有课程模型、订单模型和优惠券模型.这是模型中的关联

I have really been scratching my head on this and would greatly appreciate help. I have a store setup where people can take courses. I have a course model, order model, and coupon model. Here are the associations in the models

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

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

I have a very simple coupon model setup that has code and newprice columns. I want the ability for someone to be able to fill out the coupon form on the new order page and it to update the price.

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

In my my view for new order I have two forms one for the new order and one for the coupon. How do check in my controller if a user has entered the correct coupon code? How do I update the coupon price to be shown instead of the course price?

这是我的订单控制器

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

推荐答案

您可以通过使用 form_for 帮助器,带有 :remote 选项.

You can accomplish this with an AJAX request using the form_for helper with the :remote option.

  1. 为您的 coupons 表单将 :remote 选项设置为 true 以提交 AJAX 请求.
  2. 创建控制器操作以处理来自表单的 AJAX 请求.
  3. 使用 JavaScript 响应控制器操作,以使用新的价格信息等更新您的 orders 表单(您视图中的另一个表单).
  1. Set :remote option to true for your coupons form to submit the AJAX request.
  2. Create controller action to handle the AJAX request from the form.
  3. Use JavaScript to respond to the controller action to update your orders form (the other form in your view) with the new price information, etc.

使用`:remote`的AJAX请求

以下是一些代表您的 coupon 表单的示例代码:

<%= 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 %> 

注意以下几点:

  • form_for 标记的 :remote 选项设置为 true.
  • :url 选项是 CouponsController 中控制器操作的路径.由于 :remote 选项设置为 true,请求将作为 AJAX 请求发送到此 :url 选项.
  • 在这个代码示例中,假设它在 routes.rb 文件中定义了一个路由来处理检查优惠券代码的 AJAX 请求:
    • post 'check_coupon_code' =>'优惠券#check_coupon_code'
    • 注意:在 forms_for 帮助器中,:url 选项将 _path 附加到 routes.rb<中定义的前缀/代码> 文件.
    • 附注:使用命令 rake routes 查看可用路由及其各自的控制器操作目标.
    • The :remote option for the form_for tag is set to true.
    • The :url option is the path to your controller action in your CouponsController. Because the :remote option is set to true, the request will be posted to this :url option as an AJAX request.
    • 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'
      • Note: In the forms_for helper, the :url option appends _path to the prefix defined in the routes.rb file.
      • Bonus note: Use the command rake routes to see the available routes and their respective controller action targets.

      在您的 CouponsController 中,定义操作 check_coupon_code 来处理来自上述 form_for 的 AJAX 请求:

      In your CouponsController, define the action check_coupon_code to handle your AJAX request from the above 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
      

      注意操作的 respond_to 块中的 format.js.这允许控制器使用 JavaScript 响应 AJAX 请求以更新视图中的 orders 表单.您必须定义相应的 app/views/coupons/check_coupon_code.js.erb 视图文件,该文件生成将在客户端发送和执行的实际 JavaScript 代码(或将 JavaScript 文件命名为check_coupon_code.js.coffee(如果您使用的是 CoffeeScript).

      Notice the format.js in the respond_to block of the action. This allows the controller to respond to the AJAX request with JavaScript to update your orders form in your view. You'll have to define a corresponding app/views/coupons/check_coupon_code.js.erb view file that generates the actual JavaScript code that will be sent and executed on the client side (or name the JavaScript file check_coupon_code.js.coffee if you're using CoffeeScript).

      check_coupon_code.js.erb 文件中的 JavaScript 然后将更新您的 order 表单中的价格.

      The JavaScript in your check_coupon_code.js.erb file will then update the price in your order form.

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

      WARNING: Even if you use JavaScript to change the order price on the client-side (i.e. the browser), it is critical to validate the actual price again in the back-end (i.e. in your controller) in case some malicious user tries to manipulate the browser's request, etc.

      您可以查看官方 RailsGuide 以获取另一个示例.

      You can see the official RailsGuide for another example.

      这篇关于Ruby on rails 和优惠券模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆