Learn An Easy Way to Show Nutrition Facts in your Rails App

with metasyncro

H4zouacdsuac08ohgsvx?cache=true

Problem & Solution for Adding Nutrition Facts to Apps

Problem 

You’re building a rails app that is required to display nutrition data for millions of potential foods. 

Solution 

In this lesson, I will show you how to integrate the free tier of the Nutritionix API into rails. This code is taken directly from a popular recipe building app and it can do the following: 

  1. Search Through a Database of Foods 
  2. Display Food Names and Calories (or hundreds of other nutrition facts)
  3. Save the Food to the Database. (Avoiding future API calls). 

My goal is to give you a proven foundation for building a rails-based nutrition app that is easy to modify and expand. I breakdown every method in detail so there’s no confusion about what is happening with the code. 

Prerequisites

- Basic Knowledge of Ruby on Rails
- Basic Knowledge of HTML/CSS and Javascript
- Rails 4+ 

Initial Rails Configuration for Nutritionix

Starting with a basic rails app, you can using the following commands to get nutrition facts into your app. 

Get Free Nutritionix API Keys

First, you'll need an account to access the Nutritionix API. Head over to https://developer.nutritionix.com/docs/v1_1 and get your keys. 

Initial App Steup

Gemfile

gem 'nutritionix'
gem 'figaro' # to handle API keys securely
Then from the terminal run 

$ bundle
$ figaro install

config/application.yml

nutritionix_app_id: 'YOUR_API_ID'
nutritionix_app_key: 'YOUR_API_SECRET'

App Structure

Our App is going to allow users to do two key things: 

(1) Search the Nutritionix API by keyword. 

(2) Save the food to the database with the the calorie count. 

To keep things simple, we will only save the number of calories, but this method can be used to extract any nutrition data from the API, such as daily value percentages, fat, vitamin daily values, etc. 

In the terminal, run: 

rails generate scaffold Food name:string calories:float

Model Methods to Search the API and Parse JSON Results

This Section is Locked!

Unlock this lesson for $2 to view all sections.

Showing Results in the View

This Section is Locked!

Unlock this lesson for $2 to view all sections.

Model, View, and Controller Files for Nutritionix Lesson

This Section is Locked!

Unlock this lesson for $2 to view all sections.

Signup and Unlock for $2

Grades

Graded

It does not work with my rails app. I keep getting unitized constant errors along with routing errors

Av 1 6de6c68998209aa73ceb4952e20f35de3e0e321b80400da723550f17f6175432
a

Graded

Helped me!

Av 0 74a870efc51d61dcf45c95a53df6dcc47ab8a5454eb74082537ac6610a75b2ac
c

Graded

Good information, but needs to be updated and should elaborate more on code.

Av 0 74a870efc51d61dcf45c95a53df6dcc47ab8a5454eb74082537ac6610a75b2ac
a 90.0%
Technology Code

  • 7 Unlocks
  • 326 Total Reads
  • 20 minutes Est. Learning Time
Top Rated