3

I am getting JSON in following format from my Android application

{"list":[{"itemno":"w01","name":"t01","amount":"120","status":"done"},{"itemno":"w02","name":"t02","amount":"120","status":"done"},{"itemno":"w03","name":"t03,"amount":"120","status":"done""}]}

I need to parse this to insert into mysql table "list". I modified "create" code in my controller 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

But its unable to parse the JSON, so not calling the create method. I am very new to ROR and tried above code too from web. Not sure if the entire piece above is incorrect or I am missing something. Please advise. Thanks.

1 Answer 1

3

Also, you can refactor your code a little bit:

# This part of code
@lists = []
lists.each do |list|
  list.save
  @lists << list
end
# Can be shortened:
lists.map(&:save)
@lists = lists

The whole action can be refactored like this:

def create
  lists = params[:list].each{ |list_attributes| List.new(list_attributes) }
  valid, not_valid = lists.partition{ |list| list.valid? }

  if not_valid.blank?
    lists.map(&:save)
    @lists = lists
    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
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks. Tried with suggested changes but still getting error "TypeError (no implicit conversion of Array into String):"
From what line, what method that it is raised? If it is from the JSON.decode, it means your params[:list] are already converted into Array (of Hashes).
Yes its from JSON.decode. If its already converted into Array, could you please advise what to do next. I am totally new to ROR so bit confused how to proceed. Thanks
Thanks a lot again. I tried with updated answer and its parsing the JSON object into 3 and creating 3 Insert Statements. But the values are populating as NULL. The Insert statement is taking values as NULL. INSERT INTO lists (amount, itemno, name, status) VALUES (NULL, NULL, NULL, NULL)
Okay then try to see the output of lists.first to see how the data is formated and if it matches the Model's attributes
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.