Ruby on rails和优惠券模型

Jos*_*osh 1 ruby-on-rails coupon

我真的一直在摸不着头脑,非常感谢帮助.我有一个商店设置,人们可以在那里上课.我有一个课程模型,订单模型和优惠券模型.以下是模型中的关联

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
Run Code Online (Sandbox Code Playgroud)

我有一个非常简单的优惠券模型设置,包含代码和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
Run Code Online (Sandbox Code Playgroud)

ric*_*inn 6

您可以使用form_for带有:remote选项帮助程序通过 AJAX请求完成此操作.

摘要

  1. 设置:remote选项以true使coupons表单提交AJAX请求.
  2. 创建控制器操作以处理来自表单的AJAX请求.
  3. 使用JavaScript响应控制器操作,orders使用新的价格信息等更新您的表单(视图中的另一个表单).

使用`: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 %> 
Run Code Online (Sandbox Code Playgroud)

请注意以下事项:

  • 标记的:remote选项form_for设置为true.
  • :url选项是您的控制器操作的路径CouponsController.因为该:remote选项设置为true,所以请求将:url作为AJAX请求发布到此选项.
  • 在这个代码示例中,它假设它在routes.rb文件中有一个这样定义的路由来处理检查优惠券代码的AJAX请求:
    • post 'check_coupon_code' => 'coupons#check_coupon_code'
    • 注意:在forms_for帮助程序中,该:url选项附加_pathroutes.rb文件中定义的前缀.
    • 附加说明:使用此命令rake routes查看可用路径及其各自的控制器操作目标.

在Controller中处理AJAX请求

在你的中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
Run Code Online (Sandbox Code Playgroud)

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

使用JavaScript进行更新

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

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

您可以在另一个示例中看到官方的RailsGuide .