【问题标题】:404 Not Found error on ngResource delete in AngularJS & Rails在 AngularJS 和 Rails 中删除 ngResource 时出现 404 Not Found 错误
【发布时间】:2015-08-31 16:12:15
【问题描述】:

我在 AngularJS 中有一个用于帐户页面的帐户控制器,并且在此页面上显示与该帐户相关的记录。

页面的url是基于Account的:

http://localhost:3000/#/accounts/16

在我的帐户控制器中,我检索记录:

            Account = $resource('/accounts/:accountId', {
                accountId: "@id",
                format: 'json'
            }, {
                'save': {
                    method: 'PUT'
                },
                'create': {
                    method: 'POST'
                }
            });

            var Record = $resource('/records/:recordId', {
                recordId:'@id',
                format: 'json'
            }, {
                'save': {
                    method: 'PUT'
                },
                'create': {
                    method: 'POST'
                },
                'delete': {
                    method: 'DELETE'
                }
            });

            if ($routeParams.accountId) {
                Account.get({
                    accountId: $routeParams.accountId
                }, (function(account) {
                    return $scope.account = account;
                }), (function(httpResponse) {
                    $scope.account = null;
                    return flash.error = "There is no account with ID " + $routeParams.accountId;
                }));

                Record.query({
                    account_id: account_id
                }, function(results) {
                    return $scope.records = results;
                });

            } else {
                $scope.account = {};
                $scope.records = [];
            }

它们显示在“帐户”页面上:

    <tbody>
        <tr ng-repeat="record in records">
            <td>{{record.name}}</td>
            <td>{{record.description}}</td>
            <td>
                <button type="submit" ng-click="deleteRecord(record)">Delete</button>
            </td>
        </tr>
    </tbody>

删除方法:

            $scope.deleteRecord = function(record) {

                record.$delete(function() {
                    return flash.success = "Record deleted successfully.";
                });

                var records = $scope.records;

                for (var recordKey in records) {
                    if (records[recordKey]._id == record._id) {
                        $scope.records.splice(recordKey, 1);
                    }
                }
                return;

            };

我的问题是尝试删除记录时出现以下错误:

DELETE http://localhost:3000/records/2    [HTTP/1.1 404 Not Found  69ms]

运行rails server时终端显示如下输出:

Started DELETE "/records/2?format=json" for 127.0.0.1 at 2015-06-15 21:18:04 -0400
Processing by RecordsController#destroy as JSON
  Parameters: {"id"=>"2"}
  Record Load (4.5ms)  SELECT  "records".* FROM "records" WHERE "records"."id" = $1 LIMIT 1  [["id", 2]]
  Rendered text template (0.0ms)
Completed 404 Not Found in 12ms (Views: 4.7ms | ActiveRecord: 4.5ms)

当我不在该模型的当前页面上时,如何删除记录?

当我删除我所在页面的特定模型(即从帐户页面删除帐户)时,以这种方式删除项目。

我的控制器动作供参考:

  def destroy
    record = Record.find(params[:id])
    record.destroy!
    head :no_content
  end

感谢任何帮助。

谢谢!

【问题讨论】:

    标签: ruby-on-rails angularjs ruby-on-rails-4 rails-routing ngresource


    【解决方案1】:

    首先,我认为你不必通过一个URL参数"/records/2?format=json"来设置格式。 AngularJS 默认发送标头Content-Type: application/json。而且你的控制器也应该使用这种方式,来决定使用哪种格式。

    第二:为了更接近问题的根源,在此处放置一个调试器语句(或您正在使用的任何调试器)

      def destroy
        record = Record.find(params[:id])
        debugger
        record.destroy!
        head :no_content
      end
    

    然后进行 json 调用。当您进入终端中的调试器并且成功销毁所有内容时,您离问题更近了。

    还可以尝试为DELETE 请求发回一些真实数据(请参阅此处Is an entity body allowed for an HTTP DELETE request? 的讨论)。

    【讨论】:

    • 感谢您的回复。可悲的是,调试并没有给我很多其他线索。虽然我能够跳过这些语句,但该记录并未从数据库中删除,并且控制台错误仍然存​​在。删除 'format=json' 确实会破坏请求,所以它可能是我在特定设置中需要的东西。关于这可能是什么的任何其他想法?
    【解决方案2】:

    错误表明 rails 路线不存在。您是否检查了您的路线以确保在 routes.rb 文件中存在 DELETE 路线?
    在你的终端运行

    rake routes
    

    如果您没有看到 DELETE 路线,您可能在 routes.rb 中有类似的内容

    resources :accounts do
      resources :records, only: [:create, :index, :show]
    end
    

    将其更改为包含销毁

    resources :acounts do
      resources :records, only: [:create, :index, :show, :destroy]
    end
    

    【讨论】:

      猜你喜欢
      • 2018-07-19
      • 2017-09-03
      • 1970-01-01
      • 2017-11-25
      • 2016-10-05
      • 1970-01-01
      • 2017-10-19
      • 2018-12-24
      • 2012-09-27
      相关资源
      最近更新 更多