Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 保存rails中接收的json数据时出现问题_Ruby On Rails_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 保存rails中接收的json数据时出现问题

Ruby on rails 保存rails中接收的json数据时出现问题,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,以下是我的json数据: { "authenticity_token" =>"pdXG/h4Ds0g2ysC564mNSfGe7HfpPWHD+X81u06hJFI=", "utf8" =>"✓", "ar_identifier" =>"14433", "bji" => { "date" =>"02-12-2013", "ar_bji_o_involveds" =>

以下是我的json数据:

{
   "authenticity_token"   =>"pdXG/h4Ds0g2ysC564mNSfGe7HfpPWHD+X81u06hJFI=",
   "utf8"   =>"✓",
   "ar_identifier"   =>"14433",
   "bji"   =>   {
      "date"      =>"02-12-2013",
      "ar_bji_o_involveds"      =>      {
         "0"         =>         {
            "name"            =>"h",
            "dob"            =>"",
            "charge"            =>"j"
         },
         "1"         =>         {
            "name"            =>"jkljlkj",
            "dob"            =>"",
            "charge"            =>"jkljklj"
         }
      }
   }
}
我需要循环和保存姓名,出生日期和费用。请不要0,1是不固定的,他们的大小可以达到任何数字

我正在尝试这个:

     counter = 0;
     other = params[:bji][:ar_bji_o_involveds]

     other.each do |other|
       e = MyModel.new(params[:bji][:ar_bji_o_involveds][:counter])
       e.save
       counter+= 1
     end
但保存的数据为空。请帮助我。

您可以尝试以下操作:

 other = params[:bji][:ar_bji_o_involveds]

 other.each do |key, value|
   e = MyModel.new(value)    #Assuming your model has `dob`, `charge` and `name` fields
   e.save!  
 end

不需要
计数器

非常感谢您的快速回复。这就是问题所在:)