发布请求随机失败:Rails字符串

时间:2014-02-25 04:25:50

标签: ruby-on-rails activerecord

我有一个记录表,其日期列的类型为字符串。该模型验证了日期的存在。当我尝试POST到创建新录音的路线时,我得到了各种奇怪的回应......包括成功。下面的所有内容都会一直返回这些响应,因此我认为这不是我把它们放入的时间或顺序的问题。我也相信params包含“date”键并且与服务器之后的下面的字符串相关联告诉我它接收的参数。

“1234”或“53”返回

ArgumentError (argument out of range)

“123”,“12”,“2月”,“feb”返回

work

不太相关,因为我不能用更长的字符串重复它

“1”,任何两个字母(无论是否为大写),“男人”或“男人”都会返回     {     “错误”:     “日期不能为空”     ]     } 2月/部分有效但男人/男人会收到有关不存在日期的错误。

我删除了数据库。我已经将他们重新塑造了。然后我再次通过Cap进行部署。奇怪的是,但它在我的本地服务器上根本不存在。我很茫然。

编辑:

超出范围的失败500参数的示例

I, [2014-02-25T04:39:49.362396 #8892]  INFO -- : Processing by API::V1::PatientsController#show as JSON
I, [2014-02-25T04:39:49.362615 #8892]  INFO -- :   Parameters: {"token"=>"VgfDqgsX1wt9zt-hEXnU", "id"=>"0"}
I, [2014-02-25T04:39:49.494759 #8892]  INFO -- : Completed 200 OK in 132ms (Views: 0.6ms | ActiveRecord: 17.2ms)
I, [2014-02-25T04:40:06.813557 #8895]  INFO -- : Started POST "/api/v1/recording?token=kVqFKuj3kpX7aX3UUQft&date=02-24-2014_19-39-03_PST&position=left&image_file=whatever.img&sound_file=sound.mp3&PID=ab" for 74.51.144.67 at 2014-02-25 04:40:06 +0000
I, [2014-02-25T04:40:06.815150 #8895]  INFO -- : Processing by API::V1::RecordingsController#create as JSON
I, [2014-02-25T04:40:06.815498 #8895]  INFO -- :   Parameters: {"token"=>"kVqFKuj3kpX7aX3UUQft", "date"=>"02-24-2014_19-39-03_PST", "position"=>"left", "image_file"=>"whatever.img", "sound_file"=>"sound.mp3", "PID"=>"ab"}
I, [2014-02-25T04:40:06.821460 #8895]  INFO -- : Completed 500 Internal Server Error in 6ms
F, [2014-02-25T04:40:06.823280 #8895] FATAL -- :
ArgumentError (argument out of range):
  app/controllers/api/v1/recordings_controller.rb:15:in `create'

成功的一个例子

I, [2014-02-25T04:44:10.637765 #8892]  INFO -- : Processing by API::V1::PatientsController#show as JSON
I, [2014-02-25T04:44:10.637979 #8892]  INFO -- :   Parameters: {"token"=>"VgfDqgsX1wt9zt-hEXnU", "id"=>"0"}
I, [2014-02-25T04:44:10.778589 #8892]  INFO -- : Completed 200 OK in 140ms (Views: 0.7ms | ActiveRecord: 24.0ms)
I, [2014-02-25T04:44:26.323157 #8895]  INFO -- : Started POST "/api/v1/recording?token=kVqFKuj3kpX7aX3UUQft&date=nov_10th_2013-12:00&position=left&image_file=whatever.img&sound_file=sound.mp3&PID=ab" for 74.51.144.67 at 2014-02-25 04:44:26 +0000
I, [2014-02-25T04:44:26.324878 #8895]  INFO -- : Processing by API::V1::RecordingsController#create as JSON
I, [2014-02-25T04:44:26.325112 #8895]  INFO -- :   Parameters: {"token"=>"kVqFKuj3kpX7aX3UUQft", "date"=>"nov_10th_2013-12:00", "position"=>"left", "image_file"=>"whatever.img", "sound_file"=>"sound.mp3", "PID"=>"ab"}
I, [2014-02-25T04:44:26.390239 #8895]  INFO -- : Completed 200 OK in 65ms (Views: 0.2ms | ActiveRecord: 36.8ms)

修改

控制器代码+完成请求的完成

  def restrict_access_by_token
    @current_user = User.find_by_authentication_token(params[:token])
    if @current_user.blank?
      render json: {content: 'No user with that authentication token exists.',}, status: 401
    end
    @current_user = @current_user[0]
  end

  def permit_params
    params.permit(:date, :position, :sound_file, :image_file, :audo_data_points, :patient_id, :token, :format, :PID)
  end


 def create 
    @recording = Recording.new(permit_params.delete_if { |k,v| ["token", "format", "PID"].include? k })
    if @recording.save
      @recording.update_attribute("user_id", @current_user.id)
      possible_patients = Patient.where("user_id = ?", @current_user.id)
      patient = possible_patients.find{|patient| (patient.identifier.decrypt ENV['STRONGBOX']) == params["PID"]}
      if patient.blank?
        render json: {error: "Need a PID param to associate recording with."}, status: 401
      else
        patient.recordings << @recording
        @recording = decrypt_hash(@recording)
        render json: {recording_id: @recording["id"]}, status: 200
      end
    else
      render json: {errors: @recording.errors.full_messages}, status: 401
    end
  end

  def decrypt_hash(object)
    hash = object.attributes
    if object.class == Patient
      return decrypt(object, hash, ["identifier", "first_name", "last_name", "dob"])
    elsif object.class == Note
      return decrypt(object, hash, ["content"])
    elsif object.class == Recording
      return decrypt(object, hash, ["sound_file", "image_file"])
    end
  end

  def decrypt(object, hash, encrypted_files)
    hash.map do |k, v|
      if encrypted_files.include? k
        hash[k] = object.send(k).decrypt ENV["STRONGBOX"]
      end
    end
    delete_extra_encryption_fields(hash)
  end

  def delete_extra_encryption_fields(hash)
    delete_array = []
    hash.each do |k, v|
      delete_array << k + "_key"
      delete_array << k + "_iv"
    end
    hash.delete_if { |k,v| delete_array.include? k }
  end

0 个答案:

没有答案