【问题标题】:GETjson 500 (Internal Server Error)GETjson 500(内部服务器错误)
【发布时间】:2015-09-14 16:53:52
【问题描述】:

我正在尝试使用 Angular 呈现所有用户的列表,但是当我尝试加载 users.json 时出现 500 内部服务器错误。

这是我的服务, angular.module('users')

  .factory('UsersFactory', ['$http', function($http) {
    return {
      loadUsers: function() {
        return $http.get('/users.json');
      }
    };
  }])

这是控制器, angular.module('users', [])

  .controller('UserListCtrl', [

    '$scope', 'UsersFactory',
    function ($scope, UsersFactory) {

      var init = function(){
        UsersFactory.loadUsers().then(function(response) {
          $scope.users = response.data;
          console.log ($scope.users)
        });
      }
      init();

    }
  ]);

以及我尝试呈现用户的模板,

Users
.user_container{"ng-repeat" => "user in user"}
  {{ user.name }}

这是我的 Rails 服务器中的错误输出,

Completed 500 Internal Server Error in 27ms (ActiveRecord: 22.9ms)

ActionView::MissingTemplate (Missing template users/index, application/index with {:locale=>[:en], :formats=>[:json], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :haml, :jbuilder]}. Searched in:
  * "/home/alucardu/sites/movieseat/app/views"
  * "/home/alucardu/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/devise-3.5.2/app/views"
):

【问题讨论】:

  • 该错误与您的服务器端代码和角度代码有关,因此最好将服务器端代码放在问题中并从问题中删除js代码
  • @entre 感谢您的意见。有道理。你能帮我设置控制器和路由来解决这个问题吗?
  • 对不起,我对 ror 不了解,所以帮不上忙

标签: ruby-on-rails angularjs


【解决方案1】:

这样说,

@users = User.all
  respond_with(@users)

在我的 user_controller.rb 和这个

resources :users, only: [:index, :show]

在我的 routes.rb 中,我让它工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    • 2011-10-17
    • 2010-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多