Rails and Ionic Make Love Flashcards
Setup Project
$mkdir blog_app_project
$cd blog_app_project
Setup Basic Rails App
$rails new BlogApp $cd BlogApp $mv BlogApp web $bundle install $rails g scaffold BlogEntries title:string content:text $rake db:migrate
Active Model Serializers: use the AMS gem when building RESTful API’s in Rails
blog_entry_seriailizer.rb
#/web/app/serializers/blog_entry_seriailizer.rb class BlogEntrySerializer
Active Model Serializers
active_model_serializer.rb
Ionic uses Angular,by default receive data w/o root node. Need to disable the root node in the JSON output.
#/web/config/initializers/active_model_serializer.rb ActiveSupport.on_load(:active_model_serializers) do # Disable for all serializers (except ArraySerializer) ActiveModel::Serializer.root = false # Disable for ArraySerializer ActiveModel::ArraySerializer.root = false end
Test the Rails JSON API
$rails s
localhost: 3000/blog_entries/new
localhost: 3000/blog_entries.json
Setup Rack Cors
Installin Gem #/web/Gemfile
gem ‘rack-cors’, :require => ‘rack/cors’
Setup Rack Cors
Configuring the middleware #/web/config/application.rb
config.middleware.insert_before 0, “Rack::Cors” do
allow do
origins ‘’
resource ‘’, :headers => :any, :methods => [:get, :put, :delete, :post, :options]
end
end
Create The Ionic App ionic start ionic_blog_app tabs
mv ionic_app mobile
- blog_app_project
- web
- … rails app
- mobile
- … ionic app
Angular $resource
Ionic and RESTful resources we tend to use the Angular $resource service.
Angular $resourceAnd inject ngResource into your app module:
/ /mobile/www/js/app.js /
angular.module(‘starter’, [‘ionic’, ‘starter.controllers’, ‘starter.services’, ‘ngResource’])
add the BlogEntry factory Note: for production apps we wouldn't hardcode these URL's, but rather have a module that determines whether to fire requests to the production, staging or the local environment.
/ /mobile/www/js/services/js /
.factory(‘BlogEntry’, function($resource) {
return $resource(“http://localhost:3000/blog_entries/:id.json”);
})
Update Ionic Controller and Route
pull our blog entries JSON from the Rails app and display it in the Ionic app
first ‘Status’ tab. This Status tab is named as the DashCtrl and tab-dash.html t
/ /mobile/www/js/controllers.js: /
.controller(‘DashCtrl’, function($scope, BlogEntry) {
BlogEntry.query().$promise.then(function(response){
$scope.blog_entries = response;
}); })
Injecting the BlogEntry factory into the DashCtrl.
Query the BlogEntry service, returns an Angular $promise as the asynchronous toRails app.
Rails app responds and this promise is resolvedreturned collection to $scope.blog_entries and available to our view.
blog_entries con the scope, can interate over the collection in tab-dash.html.
/ /mobile/www/templates/tab-dash.html /
<div class="list card"> <div class="item item-divider">{{blog_entry.title}}</div> <div class="item item-body"> <div> {{blog_entry.content}} </div> </div> </div>
Install Devise in your Rails App
$rake db:migrate
gem ‘devise’
$bundle install
$rails generate devise:install
$rails generate devise User
Changes to Devise
# web/config/environments/development.rb config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
Adding our custom Devise controllers to the routes.
# web/app/config/routes.rb: devise_for :users, :controllers => {sessions: 'user/sessions', registrations: 'user/registrations', passwords: 'user/passwords' }