【问题标题】:rails internal server error when do ajax post进行ajax发布时出现内部服务器错误
【发布时间】:2015-11-21 19:07:19
【问题描述】:

我是一名新的 Rails 开发人员。因此,当我尝试向我的控制器发送 AJAX 发布请求时,我收到 500 Internal server error。有我的 Rails 控制器

class MainController < ApplicationController
    before_action :authenticate_user!, except: :index
    def index
      @auctions = Auction.last(5)
    end

    def rate
      @auction = Auction.find(params[:id])
      if current_user.rates_count > 0
         @auction.user = current_user
         @auction.save
         current_user.rates_count -= 1
         current_user.save
         @auction.user.login
      else
         'Error'
      end
    end
end

这是我的 application.js 代码

$(document).ready(function() {
$(".products-bet").click(function() {
    $.ajax({
        type: 'POST',
        url: '/rate',
        data: {'id': auct_id }
    });
});$(document).ready(function() {
$(".products-bet").click(function() {
    $.ajax({
        type: 'POST',
        url: '/rate',
        data: {'id': auct_id }
    });
});  });

这是我的路线.rb

Rails.application.routes.draw do
    mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
    devise_for :users, :controllers => { registrations: 'registrations' }
    root 'main#index'
    post 'rate', to: 'main#rate'
end

所以,我无法理解我做错了什么。

【问题讨论】:

  • 你能发布你的错误日志吗?

标签: javascript ruby-on-rails ajax routes internal-server-error


【解决方案1】:

可能有很多原因 - 您收到 500 错误意味着它可能正确路由,否则您会收到 404。可能是 @auction 为 nil 或 current_user 没有 rates_count 方法或属性

但是通过查看@你的 development.log 文件应该很容易调试。一般来说,使用 ajax,您可以通过删除路由中调用的“post”限制轻松调试,然后只需通过在浏览器中键入进行调用,以便堆栈跟踪显示在浏览器上。

您也可以尝试使用调试器来单步调试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 2019-05-16
    • 2012-06-12
    • 1970-01-01
    相关资源
    最近更新 更多