【问题标题】:Ruby on rails and coupon modelRuby on rails 和优惠券模型
【发布时间】:2014-06-10 06:29:45
【问题描述】:

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

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

【问题讨论】:

  • 如果您想动态更新订单价格,您可能需要在优惠券订单表单中使用一些 Javascript (AJAX)。您可以将优惠券表单作为 AJAX 请求提交给优惠券控制器。然后,如果优惠券代码正确,您可以在视图中更改订单对象的形式(和价格)。
  • 这很有意义。你有任何我可以参考的代码来实现这一点吗?
  • 在新方法中,您要查询课程两次。

标签: ruby-on-rails coupon


【解决方案1】:

您可以使用 form_for helper with the :remote option 通过 AJAX 请求完成此操作。

总结

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

AJAX 请求使用 `:remote`

下面是一些代表您的 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' =&gt; 'coupons#check_coupon_code'
    • 注意:在forms_for 帮助程序中,:url 选项将_path 附加到routes.rb 文件中定义的前缀。
    • 注意事项:使用命令 rake routes 查看可用路由及其各自的控制器操作目标。

在 Controller 中处理 AJAX 请求

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

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 代码(如果您使用的是 CoffeeScript,则将 JavaScript 文件命名为 check_coupon_code.js.coffee)。

使用 JavaScript 更新

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

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

您可以查看另一个 example 的官方 RailsGuide。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    • 2016-04-20
    • 2019-07-29
    • 1970-01-01
    • 2011-06-01
    相关资源
    最近更新 更多