LMo
LMo

Reputation: 1429

Can't get JSON from Ruby on Rails Ajax call

I'm trying to hit the server to deliver back a category's category_scores. Right now, when I attempt to hit the server, it gives me back an error in the console:

 GET http://localhost:7000/categories/3/category_scores 500 (Internal Server Error)

On the homepage, I have a select menu via Rails that gives all of the categories.

<div id="category-select">
  <%= collection_select(:id, :name, @categories, :id, :name, prompt: "Select category") %>
</div>

To give you some more background, here are the models for a category and the category_score

CATEGORY

class Category < ActiveRecord::Base
  has_many :category_scores

  validates :name, presence: true

  include PgSearch
  multisearchable against: :name


  def as_json(data)
    {
      id: id,
      gg: gg,
      name: category.name
    }
  end
end

CATEGORY SCORES

class CategoryScore < ActiveRecord::Base
  belongs_to :restaurant
  belongs_to :category
  has_many :items

  scope :health_order, -> {order("category_scores.gg DESC")}

  def as_json(data)
    {
      id: id,
      gg: gg,
      name: category.name
    }
  end
end

I added a custom route to my routes file:

ROUTES

  get 'categories/:category_id/category_scores', to: 'categories#scores'

Category's controller

class CategoriesController < ApplicationController

  def scores
    category = Category.find(params[:category_id])
    render json: category.category_scores.all, status: :ok
  end

end

Then finally, here is the JavaScript I am using to try and hit the server to get the JSON. I'm guessing nothing is wrong with this, but something I'm doing on the back end since I can't get the JSON to appear. Right now when I console.log the categoryID, it does appear as correct, so I'm not sure why it isn't carrying through via the controller.

$(document).ready(function() {
  var categorySelect = $("#category-select > select");

  categorySelect.on("change", function(event) {
    var categoryId = parseInt($("#category-select option:selected").val(), 10);
    console.log(categoryId)

    $.get("/categories/" + categoryId + "/category_scores", function(data){
      console.log(success);
      renderCategoryScores(data); // render category scores with this category ID
    })
      .fail(function(){
        console.log("error");
      })
  })
});

Here are the errors from the rails console:

Started GET "/categories/2/category_scores" for 127.0.0.1 at 2014-10-30 13:33:27 -0700
Processing by CategoriesController#scores as */*
  Parameters: {"category_id"=>"2"}
Completed 500 Internal Server Error in 3ms
** [Airbrake] Notice was not sent due to configuration:         
  Environment Monitored? false         
  API key set? false

NameError (undefined local variable or method `category' for #<CategoriesController:0x007ff1040a6778>):
  app/controllers/categories_controller.rb:4:in `scores'

Upvotes: 1

Views: 199

Answers (1)

srt32
srt32

Reputation: 1270

I think you'll need to define category.

class CategoriesController < ApplicationController

  def scores
    category = Category.find(params[:category_id])
    render json: category.category_scores.all, status: :ok
  end

end

Upvotes: 1

Related Questions