2014-10-30 3 views
1

나는 카테고리의 category_scores를 돌려주기 위해 서버를 치려고한다. 내가 서버를 공격 할 때 지금, 그것은 콘솔에 오류가 날 다시 제공 : 홈페이지에Ruby on Rails Ajax에서 JSON을 얻을 수 없다.

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

을, 나는 모든 범주를 제공 레일을 통해 선택 메뉴가 있습니다.

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

하는 당신에게 좀 더 배경을 제공하기 위해, 여기에 카테고리와 category_score의 모델은 내가 정의 경로를 추가

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 

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 점수입니다 내 경로 파일 :

ROUTES

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

종류의 컨트롤러

class CategoriesController < ApplicationController 

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

end 

그리고 마지막으로, 여기에 내가 시도하고 JSON을 얻기 위해 서버를 공격하기 위해 사용하고있는 자바 스크립트입니다. 나는 이것으로 아무 문제가 없다고 추측하지만 JSON을 표시 할 수 없기 때문에 백엔드에서 수행중인 작업이 있습니다. 지금 내가 console.log에 categoryID를 표시하면 올바른 것으로 나타나므로 컨트롤러를 통해 전달되지 않는 이유가 확실하지 않습니다. 난 당신이 category을 정의해야합니다 생각

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' 
+0

더 자세한 백 추적을 포함 할 수 있습니까? – srt32

+0

오류 추적을 게시하고 오류 선이 무엇인지 나타내는 경우 도움이됩니다. –

+0

방금 ​​레일 오류를 추가했습니다. 오류 라인은 NameError로 시작합니다. – LMo

답변

1

: 여기

$(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"); 
     }) 
    }) 
}); 

은 레일 콘솔에서 오류가 있습니다.

class CategoriesController < ApplicationController 

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

end