Build a RESTful JSON API With Rails 5 – Part One -Jan 31, 2017
How to Make a Rails 5 App API-Only June 30, 2016
Building a JSON API with Rails 5 - September 24, 2015
http://stackoverflow.com/questions/42688328/ruby-on-rails-api-for-beginner-json-response
$ gem install rails
$ rails new my_app --api
($RAILS_ROOT stands for the root directory of your rails app), then
$ bundle install
config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :patch, :put, :delete, :options]
end
end
$ rails g controller foo
def create
foo = params[:foo]
# Do whatever you want with foo
render json: {value: 'bar'}
end
post '/foo' => 'foo#index'
in the block
$ rails s
That's all. Now you can send POST requests to http://localhost:3000/foo and see what happens.