在Rails-MySQL中接受JSON数据

时间:2013-08-05 12:33:46

标签: android ruby-on-rails json

我需要将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个列名就足够了。

另外,如果在服务器端执行任何其他步骤或仅使用适当的方法名称指向控制器类,则请告诉我。请指教。感谢。

1 个答案:

答案 0 :(得分:0)

只需提供具有值的列名称即可。验证通过后,Rails会自动保存。

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