Basic app in Rails 5 api mode

 
In Rails 5  you can create an app which will provide you only JSON API support.
To do that actually there are two ways one is through rails gem and another is from git repo of rails I prefer rails gem:

================= First way(preferable) ==================
To start with rails you need ruby version >=2.2.2 so first you need to install ruby-2.3.0 and rails 5
rvm install ruby-2.3.0
rvm use --default ruby-2.3.0
gem install rails -v 5.0.0
rails new my_app --api
cd my_app
rvm use 2.3.0
create a rvm wrapper for your application for that create two files one is .ruby-version and another is .ruby-gemset.

In .ruby-version file type the current ruby version
ruby-2.3.0
In .ruby-gemset file type the current ruby version
@my_app
now to initialize rvm wrapper
cd ..
cd -
rvm current
now you will see ruby-2.3.0@my_app
bundle install
===================== Second way =====================
git clone https://github.com/rails/rails
cd rails
bundle install
while installing  bundle  you may face some problem but it's required.
once you are done with bundle
bundle exec railties/exe/rails new ../my_app --api
 The --api flag tells Rails we want an API-only project. In the past I’ve used the Rails::API  project. Rails’ new API-only feature provides us with basically the same thing.

===================================================

then you just need to check your database.yml before running rails server.

You should check your config/application.rb file it should be there.
config.api_only =true
Adding a hero resource in your application
rails g scaffold hero name:string
rails db:create
rails db:migrate 
Add the following db/seeds.rb
Hero.create([
  { name: 'Jason' },
  { name: 'Tim' },
  { name: 'Zach' },
  { name: 'Matt' },
])
Now run:
rails db:seed
If you hit http://localhost:3000/heros.json you should get this:
[
  {
    "id": 1,
    "name": "Jason",
    "created_at": "2016-04-13T14:05:06.833Z",
    "updated_at": "2016-04-13T14:05:06.833Z"
  },
  {
    "id": 2,
    "name": "Tim",
    "created_at": "2016-04-13T14:05:06.847Z",
    "updated_at": "2016-04-13T14:05:06.847Z"
  },
  {
    "id": 3,
    "name": "Zach",
    "created_at": "2016-04-13T14:05:06.849Z",
    "updated_at": "2016-04-13T14:05:06.849Z"
  },
  {
    "id": 4,
    "name": "Matt",
    "created_at": "2016-04-13T14:05:06.852Z",
    "updated_at": "2016-04-13T14:05:06.852Z"
  }
]
Basically you have created Rails 5 app with only api mode.

Comments

Popular posts from this blog

Installing Wowza Streaming Engine on ubuntu

Highcharts with grouped categories

Completely Uninstall and Install Rubymine on ubuntu