0

I want to access the json for upvote in the browser like http://localhost:3000/groups.json (see code). Not having much luck, here is the relevant code.

Error: undefined local variable or method `group_params' for #

// 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 Controller:

   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

Additional Methods:

  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

Full Error:

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'
9
  • You're calling the set_group as a before_action for upvote, which is where I'm guessing the problem is. Can you please show the code for that method as well? Commented Mar 22, 2017 at 1:39
  • Added, the method is set the same way. It works for the index action with out problems. Commented Mar 22, 2017 at 2:08
  • Ah, so definitely not that. Can you also share the full stacktrace of the exception? Commented Mar 22, 2017 at 3:08
  • Failed to load resource: the server responded with a status of 404 (Not Found) Is what I am getting on the console. It is what I suspected the json is not being located so it must be a routing issue? I also checked the paths in the rails c. There is no path, so it must be adding a custom path for the json. I am retrieving a collection and want it accessible through route that will spit it out.....hmmm Commented Mar 22, 2017 at 4:41
  • 1
    Awesome, thanks @McDoku Commented Mar 22, 2017 at 7:09

1 Answer 1

1

To summarise our chat in comments:

According to the stacktrace the error is caused by the set_group method throwing a ActiveRecord::RecordNotFound exception which in turn results in the endpoint returning a 404, although the code snippet posted in the question for set_group does not exactly match that.

Since the upvote action does not require @group to be set you can simply remove upvote from the list of actions in before_action.

Sign up to request clarification or add additional context in comments.

1 Comment

wait no this was not the solve. I used a collection inside the group resource in router.rb making the JSON available through localhost:3000/groups/upvote.json to the AngApp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.