0

我需要将变量从我的 Android 应用程序发送到 MySQL 数据库。我在 RubyonRails 中编写了服务器端代码。我打算通过 JSON 发送变量。我现在对如何让 ROR 知道哪个值适用于哪一列感到困惑。我的控制器类如下:

    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

我在 url 中传递 'create' 方法以便执行下面的方法

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

我知道params[:category]将接受 json 数据发送,但我不确定如何让 ROR 传递所有参数。当我们有 2 列的值而不是表中定义的所有列时,可能会有这样的情况。发送 json 数据时是否需要在键中指定列名,并为未发送值的列发送空值。或者只给出特定的 2 列名称及其值就足够了。

另外,请让我知道是否需要在服务器端执行任何其他步骤或仅使用适当的方法名称定向到控制器类。请指教。谢谢。

4

1 回答 1

0

只需给出有价值的列名。验证通过后,Rails 会自动保存。

在 android 应用程序中,http://yoursite:yourport/categories使用 post 方法调用。它将执行create的方法CategoriesController。确保您可以在控制器中接收正确的 json 格式参数。

于 2013-08-12T07:56:48.360 回答