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.0create a rvm wrapper for your application for that create two files one is .ruby-version and another is .ruby-gemset.
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
In .ruby-version file type the current ruby version
ruby-2.3.0In .ruby-gemset file type the current ruby version
@my_appnow to initialize rvm wrapper
cd ..now you will see ruby-2.3.0@my_app
cd -
rvm current
bundle install===================== Second way =====================
git clone https://github.com/rails/railswhile installing bundle you may face some problem but it's required.
cd railsbundle install
once you are done with bundle
Thebundle exec railties/exe/rails new ../my_app --api
--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 =trueAdding a hero resource in your application
rails g scaffold hero name:stringAdd the following db/seeds.rb
rails db:create
rails db:migrate
Hero.create([Now run:
{ name: 'Jason' },
{ name: 'Tim' },
{ name: 'Zach' },
{ name: 'Matt' },
])
If you hitrails db:seed
http://localhost:3000/heros.json
you should get this:Basically you have created Rails 5 app with only api mode.[
{
"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"
}
]
Comments
Post a Comment