2013-08-06 35 views
0

我想通過我的Android應用程序發送JSON,在MySQL數據庫的「Category」表中創建一個新行。服務器端編碼在RubyOnRails中完成。但是我在服務器端收到Uninitialised常量錯誤。該錯誤是如下在導軌中的POST錯誤

ActionController::RoutingError (uninitialized constant CategoryController): 

我rotues.rb文件

ROMS::Application.routes.draw do 
    resources :categories 

    resources :projects 

    get "category/new" 

    get "category/index" 

    get "category/show" 

    get "category_controller/index" 

    get "category_controller/show" 

    post "category/create" 

我已經給類/創建的URL發送JSON。我的類別控制器類如下所示:

class CategoriesController < ApplicationController 
    # GET /categories 
    # GET /categories.json 
    def index 
    @categories = Category.all 

    respond_to do |format| 
     format.html # index.html.erb 
     format.json { render json: @categories } 
    end 
    end 

    # GET /categories/1 
    # GET /categories/1.json 
    def show 
    @category = Category.find(params[:id]) 

    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render json: @category } 
    end 
    end 

    # GET /categories/new 
    # GET /categories/new.json 
    def new 
    @category = Category.new 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @category } 
    end 
    end 

    # GET /categories/1/edit 
    def edit 
    @category = Category.find(params[:id]) 
    end 

    # POST /categories 
    # POST /categories.json 
    def create 
    @category = Category.new(params[:category]) 

    respond_to do |format| 
     if @category.save 
     format.html { redirect_to @category, notice: 'Category was successfully created.' } 
     format.json { render json: @category, status: :created, location: @category } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @category.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PUT /categories/1 
    # PUT /categories/1.json 
    def update 
    @category = Category.find(params[:id]) 

    respond_to do |format| 
     if @category.update_attributes(params[:category]) 
     format.html { redirect_to @category, notice: 'Category was successfully updated.' } 
     format.json { head :ok } 
     else 
     format.html { render action: "edit" } 
     format.json { render json: @category.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /categories/1 
    # DELETE /categories/1.json 
    def destroy 
    @category = Category.find(params[:id]) 
    @category.destroy 

    respond_to do |format| 
     format.html { redirect_to categories_url } 
     format.json { head :ok } 
    end 
    end 
end 

我不確定需要初始化哪些內容以在表中創建一個包含我的值的行。請指教。另外請建議,如果我需要在服務器端解析JSON以創建一個新行。我讀到這是由ROR處理,但不知道。

由於我是一個完整的新手在ROR,道歉,如果這聽起來是一個基本問題。謝謝。

回答

0

在控制器代碼中,您將其命名爲'CategoriesController'。但是在期望'CategoryController'的路線中。嘗試糾正它。

+0

謝謝。有效。實際上,我通過生成腳手架創建了Controller類,因此創建了這種方法。無論如何,我改變了我的路線文件發佈「類別/創建」,它最初發布「類別/創建」。在我的網址中也將類別更改爲類別。再次感謝。 – Gaurav

0

而不是

get "category/new" 

    get "category/index" 

    get "category/show" 

    get "category_controller/index" 

    get "category_controller/show" 

    post "category/create" 

你可以在你的控制器resources :categories

rake routes,你會得到以下航線

categories GET /categories(.:format)   categories#index 
       POST /categories(.:format)   categories#create 
new_category GET /categories/new(.:format)  categories#new 
edit_category GET /categories/:id/edit(.:format) categories#edit 
    category GET /categories/:id(.:format)  categories#show 
       PUT /categories/:id(.:format)  categories#update 
       DELETE /categories/:id(.:format)  categories#destroy 

是的,你是在端呈現JSON格式正確的類別控制器。

+0

非常感謝。這似乎很有幫助。 – Gaurav