名称错误:Angular / Rails替代控制器方法问题

时间:2017-03-22 01:26:50

标签: ruby-on-rails angularjs json

我想在浏览器中访问json for upvote,如http://localhost:3000/groups.json(参见代码)。没有太多运气,这是相关的代码。

错误:未定义的局部变量或#

的方法`group_params'
// inject $http so we can go to http://localhost:3000/groups.json in our browser and see an array of all the groups in our database
.factory('groups', ['$http', function($http){
    //service body
    var o = {
        groups: []
    };

    // get all the groups in the service, groups.js
    o.getAll = function() {
      return $http.get('/groups.json').success(function(data){
        angular.copy(data, o.groups);
      });
    };


    o.getAll = function() {
      return $http.get('/groups/upvote.json').success(function(data){
        angular.copy(data, o.groups);
      });
    };

Rails控制器:

   before_action :set_group, only: [:show, :edit, :update, :destroy, :upvote]

  def upvote
    @groups = Group.all

    respond_to do |format|
      format.html {}
      format.json { render json: @groups }
    end
  end

其他方法:

  def index
   @groups = Group.all

   respond_to do |format|
     format.html {}
     format.json { render json: @groups }
   end
  end      

 def set_group
     @group = Group.where(params[:id])
  end

完全错误:

Started GET "/groups/upvote.json" for 127.0.0.1 at 2017-03-21 21:37:22       0700
Processing by GroupsController#show as JSON
Parameters: {"id"=>"upvote"}
  Group Load (0.4ms)  SELECT  "groups".* FROM "groups" WHERE     "groups"."id" = $1 LIMIT 1  [["id", 0]]
Completed 404 Not Found in 5ms (ActiveRecord: 2.4ms)

ActiveRecord::RecordNotFound (Couldn't find Group with 'id'=upvote):
  app/controllers/groups_controller.rb:131:in `set_group'

1 个答案:

答案 0 :(得分:1)

在评论中总结我们的聊天内容:

根据堆栈跟踪,错误是由set_group方法引发ActiveRecord::RecordNotFound异常引起的,这导致端点返回404,尽管{{1}的问题中发布了代码段并不完全匹配。

由于set_group操作不需要设置upvote,您只需从@group中的操作列表中删除upvote