【问题标题】:Rails 4: Checking coupon by ajaxRails 4:通过ajax检查优惠券
【发布时间】:2017-11-27 03:39:32
【问题描述】:

我想在 Rails 中进行查询以检查 ajax 中的折扣cuom。 Ajax 工作正常,但我遇到了路由问题:

加载资源失败:服务器响应状态为 404 (未找到)/app/payment/check_cupom?cupom=afdasdf

谁能给我一盏灯?谢谢!

我有这个:

HTML

<input name="cupom" type="text" placeholder="" id="cupom">
<p id="cupomResponse"></p>

AJAX

<script type="text/javascript">
    jQuery(function($){
        $('#cupom').on('blur', function() {
          var cupom = $("#cupom").val();
          $.ajax({
              url: '/app/payment/check_cupom?cupom=' + cupom, type: 'get', dataType: 'html',
              processData: false,
              success: function(data) {
                  if (data == "record_not_found") {
                      alert("Record not found");
                  }
                  else {
                      $("#cupomResponse").val(data);
                  }
              }
          });
      });
    });
</script>

路线

resources :payment do
      collection do
        post 'pay', :action => :pay
        get 'check_cupom', :action => :check_cupom
      end
    end

控制器

def check_cupom
  @cupom = Coupon.find_by(name: params[:cupom])
  if @cupom.blank?
    render :text => "record_not_found"
  else
      discount = @cupom.price.gsub(/[^\d^\.]/, '')
      render :text => discount
  end
end

服务器日志

在 127.0.0.1 开始 GET "/app/payment/check_cupom?cupom=adfafd" 2017-06-23 11:35:21 -0300 AbstractController::ActionNotFound - The 找不到 App::PaymentController 的操作“显示”:–

【问题讨论】:

  • 不相关,但你为什么叫它cupom
  • 对不起,语言...
  • 是的。我已经尝试过 rails 路径:/
  • 你能发布你的服务器日志吗?
  • 开始 GET "/app/payment/check_cupom?cupom=adfafd" for 127.0.0.1 at 2017-06-23 11:35:21 -0300 AbstractController::ActionNotFound - 动作'show'可以找不到 App::PaymentController:

标签: jquery ruby-on-rails ruby ajax ruby-on-rails-4


【解决方案1】:

我将逻辑放在 show 函数中,它可以工作。我不知道 wyy,一旦 rake 路由显示 url 是正确的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    • 2017-02-06
    • 2018-12-14
    • 2021-04-09
    • 2016-11-10
    • 1970-01-01
    相关资源
    最近更新 更多