Hi I'm new to Ruby on Rails and I want to:
- READ JSON from URL
- GET the desired data
- ADD (create) new record to the database
1. READ JSON from URL
I have an URL that returns JSON data. You can see it here. How to read that from view (.html.erb) and controller (.rb)?
I've read this but I didn't know where and how to use it.
2. GET the desired data
If you see this json link, you'll find that there's a lot of data in it. I just want to get some of the data like product_name, image_url, description. How can I extract them?
3. ADD (create) new record to the database
I used rails g scaffold ... to create the CRUD by default. But in this case, I cannot use that. The new record must be created based on the submitted URL, not by inputting it manually.
How to do this in controller (.rb):
# POST /products or /products.json
def create
# I want something like this to read JSON
parsedJson = JSON_fromURL('http://world.openfoodfacts.org/api/v0/product/3661112054465.json')
# and I want something like this to create record
product_params.product_name = parsedJson['product_name']
product_params.image_url = parsedJson['image_url']
product_params.description = parsedJson['description']
@product = Product.new(product_params)
...
end