0
votes

I am receiving an HTTP POST from a 3rd party with content-type as 'application/x-www-form-urlencoded' in my RAILS 3.0.6 controller

I tried parsing the request both as -

job_status = params['job']['status']

as well as

recd_json = JSON.parse(params) job_status = recd_json['job']['status']

But in both cases the rails controller is throwing the error 'NoMethodError (You have a nil object when you didn't expect it! You might have expected an instance of ActiveRecord::Base.'

I am not sure how else can JSON (encoded using this content type) can be parsed..Any help is appreciated :)

Heres the json being posted: { "name": "JSON Parser", "url": "job/test/", "job": { "status": "SUCCESS", "url": "job/356/" } }

Heres the params as returned by the RAILS controller using the Rails.Logger:

{"{ \"name\": \"JSON Parser\", \"url\": \"job/test/\", \"job\": { \"status\": \"SUCCESS\", \"url\": \"job/356/\" }}"=>nil, "action"=>"receive_jobs_updates", "controller"=>"jobs"}

The Error trace:

NoMethodError (You have a nil object when you didn't expect it! You might have expected an instance of ActiveRecord::Base. The error occurred while evaluating nil.[]): app/controllers/jobs_controller.rb:32:in `receive_jobs_updates'

The controller code: def receive_job_updates

Rails.logger.info params

job_name = params['name'] job_status = params['job']['status']

render :text=> "All ok" , :status => :ok

end

1
Please paste the code of the controller action that's throwing the error, as well as the complete backtrace of the error.Veraticus
Heres the params returned by the RAILS controller:{"{ \"name\": \"JSON Parser\", \"url\": \"job/test/\", \"job\": { \"status\": \"SUCCESS\", \"url\": \"job/356/\" }}"=>nil, "action"=>"receive_job_updates", "controller"=>"jobs"} NoMethodError (You have a nil object when you didn't expect it! You might have expected an instance of ActiveRecord::Base. The error occurred while evaluating nil.[]): app/controllers/job_controller.rb:32:in `receive_job_updates'user1240755
Sorry this is hardly readable..please refer to the edited question postuser1240755

1 Answers

0
votes

You need to create a Job object: job_status = Job.new(params[:job]).status