Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to parse a JSON Object to insert values in table(MySQL). JSON is recived at server but it is unable to read the values to be inserted. Its inserting NULL values. Below is snapshot from my rails console.

Started POST "/lists.json" for 192.168.1.9 at 2013-08-13 11:38:46 +0530
  Processing by ListsController#create as JSON
  Parameters: {"list"=>[{"amount"=>"120", "status"=>"done", "itemno"=>"w01", "na
me"=>"t01"}]}
WARNING: Can't verify CSRF token authenticity
   (1.0ms)  BEGIN
  SQL (1.0ms)  INSERT INTO `lists` (`amount`, `itemno`, `name`, `status`) VALUES
 (NULL, NULL, NULL, NULL)
   (103.0ms)  COMMIT

The Create method in my lists_controller.rb is as below

def create
    lists = params[:list].collect{|key,list_attributes| List.new(list_attributes)}
    all_list_valid = true
    lists.each_with_index do |list,index|
        unless list.valid?
            all_list_valid = false
            invalid_list = lists[index]
        end
    end

    if all_list_valid
        @lists = []
        lists.each do |list|
            list.save
            @lists << list
        end
        format.html { redirect_to @list, notice: 'List was successfully created.' }
        format.json { render json: @list, status: :created, location: @list }
    else
        format.html { render action: "new" }
        format.json { render json: @list.errors, status: :unprocessable_entity }
    end
end

I am not sure why it is taking NULL values even though "Parameters" seems to have correct values. Please advise . Thanks.

share|improve this question

1 Answer 1

See documentation for the collect method.

irb(main):008:0> parameters = {"list"=>[{"amount"=>"120", "status"=>"done", "itemno"=>"w01", "name"=>"t01"}]}
=> {"list"=>[{"status"=>"done", "amount"=>"120", "itemno"=>"w01", "name"=>"t01"}]}
irb(main):009:0> parameters["list"]
=> [{"status"=>"done", "amount"=>"120", "itemno"=>"w01", "name"=>"t01"}]
irb(main):010:0> parameters["list"].collect{|list| p list}
{"status"=>"done", "amount"=>"120", "itemno"=>"w01", "name"=>"t01"}
=> [nil]  
share|improve this answer
    
Thanks for the guidance. I read the documentation and it seems that when I specify "key" its returning nil values irb(main):008:0> Parameters["list"].collect{|key,list| List.new(list)} => [#<List itemno: nil, name: nil, amount: nil, status: nil>] but when I omit "key" it returns values irb(main):007:0> Parameters["list"].collect{|list| List.new(list)} => [#<List itemno: "w01", name: "t01", amount: "120", status: "done">] Now problem is its giving NoMethodError (undefined method `stringify_keys' for ["itemno", "w01"]:Array) error –  Gaurav Aug 13 '13 at 7:46
    
Give me a code. stringify_keys method only appnds to Hash, how you get an Array from params[]? –  ostapische Aug 13 '13 at 8:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.