0

我正在尝试通过我的 Android 应用程序发送的 JSON 在 MySQL 数据库的“类别”表中创建一个新行。服务器端编码在 RubyOnRails 中完成。但是我在服务器端收到未初始化的常量错误。错误如下

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 中给出了 category/create 来发送 JSON。我的 CategoryController 类如下:

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 的新手,如果这听起来是一个基本问题,我深表歉意。谢谢。

4

2 回答 2

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。

于 2013-08-06T09:03:35.040 回答
0

在控制器代码中,您将其命名为“CategoriesController”。但在路线中,它期待“CategoryController”。尝试纠正它。

于 2013-08-06T09:06:49.303 回答