Saving external JSON to DB with Rails

前端 未结 2 464
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-15 00:50

I\'m making a GET call to an external source using the gem \'httparty\'; here\'s my Controller.rb:

def show
  response = HTTParty.get(\'URI\')
  user = JSON.         


        
2条回答
  •  天命终不由人
    2020-12-15 01:10

    Here I am assuming that you have created the uri_id field to store the external id and the name field to store the external name in the user table.

    Your Controller code -

    def show
      response = HTTParty.get('URI')
      JSON.parse(response).each do |item|
        if (item.id != nil && item.name != nil)
           u = User.new(:uri_id => item.id, :name => item.name)
           u.save
        end
      end
    end
    

提交回复
热议问题