📎 Webclip
How to Build a Rails 5 API Only and Backbone Application
The article shows how Rails API was merged into Rails core for Rails 5 and how it supports API-only applications with lightweight controllers, a reduced middleware stack, and customized generators. It then uses a simple Backbone TODO app to demonstrate the backend setup, JSON responses, and frontend integration.
Reading notes#
- Rails API was moved into Rails core, and Rails 5 will include its capabilities.
- API-only Rails apps use lightweight controllers, a reduced middleware stack, and customized generators.
- The example builds a Rails API backend for a simple Backbone TODO app.
- To generate an API-only app on the edge branch, the article uses
bundle exec railties/exe/rails new <parent-folder-path>/my_api_app --api --edge. - The generated Gemfile removes asset and template-related dependencies and includes
active_model_serializers. config/application.rbusesapi_onlyto exclude middleware and controller modules not needed for API-only apps.ApplicationControllerinherits fromActionController::API.- A Todo resource is generated with
bin/rails g scaffold todo title completed:boolean order:integer. - The scaffolded controller omits
newandedit, and it responds in JSON only. - The scaffold does not generate template files or an
app/viewfolder. resources :todoscreates only the routes needed for the API, excludingnewandeditroutes.- The database should be migrated with
bin/rake db:migrate. - A serializer is generated with
bin/rails g serializer todo title completed order. - The API can be tested with
curl, creating a todo and listing todos fromhttp://localhost:3000/todos. - The Backbone app is changed to point its todos collection to
http://localhost:3000/todosinstead of local storage. - Cross-origin requests are enabled by uncommenting
rack-corsin the Gemfile and configuringconfig/initializers/cors.rb. - The example allows requests from
localhost:9000and accepts the listed HTTP methods. - The frontend can then be served locally with
ruby -run -e httpd . -p 9000. - The article says the TODO items are then stored by the Rails API backend instead of browser local storage.
- It notes that serializer generation could be improved and mentions JBuilder as another serialization option.
