This super lightweight Rails API project will contain the following bits of awesomeness to get you started on your new app.
- Rails API (https://github.com/rails-api/rails-api)
- Sass (http://sass-lang.com)
- Coffeescript (http://coffeescript.org)
- Postgres (http://www.postgresql.org)
- Rivets.js (http://rivetsjs.com)
- Bootstrap (http://getbootstrap.com)
Create a new Rails::API (https://github.com/rails-api/rails-api) project with Postgres. Rails::API rocks for Javascript apps.
$ gem install rails-api
$ rails-api new new_project_name -T -d postgresql
Create the Postgres user
$ createuser -P -s -e new_project_name
Jump into the project folder and create the database
$ cd new_project_name
$ rake db:create
We will be using Bower to manage our front-end dependencies as it was created for this very purpose. Rubygems can probably do it, but there are a myriad of reasons it is silly! Don't worry, we will add bower-rails
gem to ensure bower dependencies are compiled with Rails' asset pipeline.
$ npm install bower
Add various gems to your Gemfile
source 'https://rubygems.org'
gem 'rails', '4.2.0.rc1'
gem 'rails-api'
gem 'pg'
gem 'active_model_serializers'
gem 'bower-rails'
# Asset Poopline!
group :assets do
gem 'coffee-script'
gem 'sass'
end
group :test, :development do
gem 'spring'
gem 'rspec-rails'
gem 'factory_girl_rails'
gem 'capybara'
gem 'database_cleaner'
end
Create a Bowerfile
to manage front-end dependencies
asset 'bootstrap-sass-official'
asset 'rivets'
# vim: ft=ruby
$ bundle install
$ rails g rspec:install
$ rake bower:install
$ mkdir app/assets/javascripts
$ touch app/assets/javascripts/application.js
//= require jquery
//= require sightglass
//= require rivets
//= require app
$ touch app/assets/javascripts/app.coffee
# Hello app!
$ ->
greeting =
heading: 'Hello!'
blurb: 'Now we are ready to get this show on the road.'
rivets.bind $('.hello'),
greeting: greeting
$ mkdir app/assets/stylesheets
$ touch app/assets/stylesheets/application.css.scss
/*
* This is a manifest file that'll automatically include all the stylesheets available in this directory
* and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
* the top of the compiled file, but it's generally better to create a new file per style scope.
*= require_self
*= require_tree .
*/
@import "bootstrap-sass-official/assets/stylesheets/bootstrap-sprockets";
@import "bootstrap-sass-official/assets/stylesheets/bootstrap";
$ touch app/controllers/home_controller.rb
class HomeController < ApplicationController
def index
render :index
end
end
$ mkdir -p app/views/home
$ touch app/views/home/index.html.erb
<!DOCTYPE html>
<html lang="en">
<head>
<title>Hello!</title>
<%= stylesheet_link_tag 'application', media: 'all' %>
<%= javascript_include_tag 'application' %>
</head>
<body>
<div class="container">
<div class="hello text-center">
<h1>{ greeting.heading }</h1>
<p class="lead">{ greeting.blurb }</p>
</div>
</div>
</body>
</html>
Update config/routes.rb
Rails.application.routes.draw do
root 'home#index'
end
$ rails s
Go to http://localhost:3000
and make sure everything is working.
If all is to your liking let's commit this. The best it yet to come!
Update your .gitignore
file with this
# Ignore stuff
/node_modules
$ git init
$ git add .
$ git commit