Ionic + Rails API (JPN) Flashcards
Rails: Create the Rails Backend
$rails new agenda_backend -T
Rails: Create the Person Model
$rails g model person name phone
Rails: Seed the test database seed.rb
Person.create(name: ‘Laura’, phone: ‘1800-3137977’)
Person.create(name: ‘Jeniffer’, phone: ‘1800-123456’)
Person.create(name: ‘Tom’, phone: ‘1800-654321’)
Rails: Add routes to Persons
namespace :api, constrains: { format: :json } do
get ‘people’ => ‘agenda#all’
end
Rails: Install Rack-Cors Gem (CORS = Cross origin reference sharing)
gem ‘rack-cors’, :require => ‘rack/cors’
Rails: Config the CORS middleware layer config > application.rb
config.middleware.use Rack::Cors do allow do origins '*' resource '*', :headers => :any, :methods => [ :get, :post, :options, :delete] end end
Rails: Install Serializer Gem
gem ‘active_model_serializers’
Rails: Generate serializer
$rails g serializer person
Rails: Config Serializer app > serializers > person_serializer.rb
class PersonSerializer
Rails: Generate API Controller
$rails g controller api/agenda all –no-helper – no-assets
Rails: Configure API Controller
module Api class Api::AgendaController
Rails: API JSON Test
$ rails s
$ curl -X GET ‘http://localhost:3000/api/people’
Ionic: Generate ionic app
$ ionic start agenda blank
Ionic: Add iOS/Android platform
$ ionic platform add ios
$ ionic platform add android
Ionic: Config People_Controller.js www > js > controllers > people_controller.js
(function() {
var app = angular.module(‘peopleController’, []);
app.controller(‘PeopleController’,
function($scope, $http) {
$scope.names = [‘Jeniffer’, ‘Jackson’, ‘Jose’];
} ); })
Ionic: Depency injection
www > js > app.js
angular.module(‘starter’, [‘ionic’, ‘peopleController’])
index.html
Ionic: Ng-Repeat for PeopleController
index.html
<ul class="list"> <li class="item"> {{name}} </li> </ul>
Rails: Find Routes for API
$rake routes
Ionic: Add route to PeopleController
people_controller.js
app.controller('PeopleController', function($scope, $http) { var url = 'http://localhost:3000/api/people'; $http.get(url) .success(function(people) { $scope.people = people; }) .error(function(data) { console.log('server side error occurred.'); }); } );
Ionic: Update the interation loop in index.html
<ul class="list"> <li class="item"> <h3>{{person.name}}</h3> <p>{{person.phone}}</p> </li> </ul>
Ionic: Emulate and Test
$ ionic serve
$ ionic emulate/run ios
$ ionic emulate/run android