i getting json in below format application
{"list":[{"orderno":"02201308231133","itemno":"w01","name":"t01","amount":"120","status":"done"},{"orderno":"02201308231133","itemno":"w02","name":"t02","amount":"120","status":"done"},{"orderno":"02201308231133","itemno":"w03","name":"t03,"amount":"120","status":"done""}]}
now need 2 tasks above json: first add above rows in lists table is, second:extract first 2 digits orderno i.e.02 in above case , update table (or call controller) status(done) value extracted (02).
i able first task. controller code same is:
def create lists = params[:list].collect{|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 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
the above method inserts rows in db table. unable figure out how parse json extract value before calling create method in lists controller. once have values need call both tables update. please advise. thanks
you can install gem 'json'
has easy-to-use methods parse json data (its documentation @ https://github.com/flori/json).
then can read json with:
json.parse(string)
which give array of string , hashes. can order number's first 2 digits iterating through such array, getting value corresponding :order
key, take first 2 digits , convert them integer using:
order_no[0,2].to_i