解析多个json对象并将其保存在Rails 4中

时间:2015-06-14 13:00:04

标签: ruby-on-rails json

我有curl命令:

 curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{
"map":
     {"lat":"123.45543","lon":"45.43424","vibration_level":"456","time_sent":"20.05.1994" } 
}' http://localhost:3000/maps

在我的创建控制器方法中我有:

def create
    @map = Map.new(map_params)
    respond_to do |format|
      if @map.save
        format.json {
          render :show, status: :created, location: @map
        }
      else
        format.json { render json: @map.errors, status: :unprocessable_entity }
      end
    end
  end

这种方法是搭建的。 Rails自动将我的JSON转换为哈希并将其正确保存到我的数据库。但是,如果我有多个这样的JSON对象,我如何解析JSON:

curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{
"map":
{"lat": "51.088672", "lon": "71.396522", "vibration_level": "300", "time_sent": "07:25:00"},
{"lat": "51.088672", "lon": "71.396453", "vibration_level": "300", "time_sent": "07:25:01"},
{"lat": "51.088829", "lon": "71.396476", "vibration_level": "300", "time_sent": "07:25:14"} 
}' http://localhost:3000/maps

当我尝试该命令时,我得到一个解析错误。

编辑: maps_controller.rb



class MapsController < ApplicationController
  before_action :set_map, only: [:show, :edit, :update, :destroy]
  skip_before_filter  :verify_authenticity_token
  before_action :parse_json_data_to_params, except: [ :index,:new,:edit,:show ]

  # GET /maps
  # GET /maps.json
  def index
    @coords = Map.all
  end

  # GET /maps/1
  # GET /maps/1.json
  def show
  end

  # GET /maps/new
  def new
    @map = Map.new
  end

  # GET /maps/1/edit
  def edit
  end

  # POST /maps
  # POST /maps.json
  def create      
    @map = Map.new(map_params)
       respond_to do |format|
      if @map.save
        format.json {
          render :show, status: :created, location: @map
        }
      else
        format.json { render json: @map.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /maps/1
  # PATCH/PUT /maps/1.json
  def update
    respond_to do |format|
      if @map.update(map_params)
        format.html { redirect_to @map, notice: 'Map was successfully updated.' }
        format.json { render :show, status: :ok, location: @map }
      else
        format.html { render :edit }
        format.json { render json: @map.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /maps/1
  # DELETE /maps/1.json
  def destroy
    @map.destroy
    respond_to do |format|
      format.html { redirect_to maps_url, notice: 'Map was successfully destroyed.' }
      format.json { head :no_content }
    end
  end


  private
    # Use callbacks to share common setup or constraints between actions.
    def set_map
      @map = Map.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def map_params
      params.permit(maps: [:lat, :lon, :vibration_level, :time_sent])
    end

  def parse_json_data_to_params
    params.merge!(ActiveSupport::JSON.decode(request.raw_post))
  end

end
&#13;
&#13;
&#13; 编辑2:

&#13;
&#13;
Started POST "/maps" for ::1 at 2015-06-14 20:56:57 +0600
Processing by MapsController#create as JSON
  Parameters: {"maps"=>[{"lat"=>"51.088672", "lon"=>"71.396522", "vibration_level"=>"300", "time_sent"=>"07:25:00"}, {"lat"=>"51.088672", "lon"=>"71.396453", "vibration_level"=>"300", "time_sent"=>"07:25:01"}, {"lat"=>"51.088829", "lon"=>"71.396476", "vibration_level"=>"300", "time_sent"=>"07:25:14"}], "map"=>{}}
Unpermitted parameter: map
   (0.1ms)  begin transaction
   (0.1ms)  rollback transaction
Completed 422 Unprocessable Entity in 8ms (Views: 0.2ms | ActiveRecord: 0.7ms)
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

在这种情况下,您也可以通过@maps = Map.create(map_params)创建它们,但现在 - 您需要更改map_params方法的定义,这是必须的。

以下是:

def map_params
  params.permit(maps: [:lat, :lon, :vibration_level, :time_sent])
end

另外一件事:将attr_accessor: maps添加到模型类中:

def Map < ActiveRecord::Base
  attr_accessor :maps
  # other code
end

修改

您在JSON结尾处传递"map"=>{},这就是您看到的错误。