We are talking about quick demos, site bootstraping kind of deal here. So don’t expect robust, detailed explanations.
Also, assuming you have Ruby / Rails / Git / postgresql / RVM (optional) installed. Barebones, cutting through most of the commands explanations. Aim to push site and ideas fast out there.
$rails new first_app -d postgresql
-d postgresql to create this with postgres
NOTE: postgresql (and role creation) have some issues. It is not a straight process, you can always default to the mysql default in your local server, and harden it on production (if saving time in a demo is an issue).
To secure your app, create a role for the postgres database specific to your app.
In your postgres admin, click on the Login Roles, add Login Role. Make sure the new role can create database objects. Make sure you set a password for it as well.
Then, in config/database.yml you need to plugin the username / password you just created ( in all the diff databases: development, test and production)
Modify your gem file as follows
$bundle install –without production –binstubs
binstubs makes it easier to run tests later (the command is shorter). It stores binaries so they don’t have to be pre-compiled everytime you run tests.
Speaking of tests, at this point you can generate the skeleton to run tests:
$rails generate cucumber:install
$rails generate rspec:install
$rake db:create
$rails server // just to make sure you have a local server setup
$git init
$git add .
$git commit -m “Initial commit”
[optional] Create a bitbucket (or github) repo:
git remote add origin https://usernamehere@bitbucket.org/usernamehere/projectnamehere
git push -u origin --all # to push up the repo for the first time
Download and install heroku toolbelt (if not there yet)
https://toolbelt.heroku.com/
$heroku login
$heroku create [name of your app here]// inside your app’s directory
// Create the basic model for your users:
$rails generate scaffold User first_name:string last_name:string
// By convention, rails likes models to be singular, and camel cased (starts with a capital letter). As in “User”
// At this point, you should have the basic CRUD operations for users at:
localhost:3000/users
// To create the user’s authentication stuff:
$ rails generate devise:install
// And follow the instructions printed afterwards (no need to replicate them here)
$ rails generate devise User
$ rails generate devise Admin
// Important! protect your user’s section by adding the following to app/controllers/users_controller.rb:
before_filter :authenticate_admin!
// And also, make sure users can’t register as admins (inside the admin’s model, app/models/admin.rb)
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable
// Notice that :registerable has been removed. If you need to create admins, you will have to do it via direct db access, or by using the console and bypassing momentarily that restriction, or, you can bring it back and ask for an extra token at admin registration time, but then you will have to manage / distribute those tokens
$bundle exec rake db:migrate // Update the DB with it
$ rails generate controller StaticPages home help about contact_us–no-test-framework // Create home and help pages, which can contain dynamic variables. It generates all your views and controllers
– Modify your public/index.html with something other than the default Rails page (or remove it if you are sending users to another page via the router)
root to: ‘static_pages#home’ // redirects “/” to static pages controller, home method
match ‘/help’, to: ‘static_pages#help’ // redirects to help page
– Put any other pure static pages in this directory as well (public/hello.html, etc)
Example of how to integrate the login and signup links in a view after devise is in place:
<% unless admin_signed_in? -%>
<p><%= link_to ‘Sign up as Admin’, new_admin_registration_path %></p>
<p><%= link_to ‘Sign in as Admin’, new_admin_session_path %></p>
<% end -%>
<% unless user_signed_in? -%>
<p><%= link_to ‘Sign up as User’, new_user_registration_path %></p>
<p><%= link_to ‘Sign in as User’, new_user_session_path %></p>
<% end -%>
<% if user_signed_in? -%>
<p><%= link_to ‘Token Authentication Example’, token_path(:auth_token => current_user.authentication_token) %></p>
<% end -%>
Example of how to protect your pages from access (via the controller):
before_filter :authenticate_user!
or
before_filter :authenticate_admin!
$rails generate scaffold Micropost content:string user_id:integer // Create an object that will be associated with a user
// Setup your basic models associations at this point:
class User < ActiveRecord::Base // Inside apps/model/user.rb
attr_accessible :email, :name
has_many :microposts
end
class Micropost < ActiveRecord::Base // Inside apps/model/micropost.rb
attr_accessible :content, :user_id
belongs_to :user
validates :content, :length => { :maximum => 140 }
end
// Next, save the changes, and push them locally and in Heroku:
$ git add .
$ git commit -m “Add models”
$ git push heroku master
$ heroku run rake db:migrate
$ heroku open // Make sure things are handy dandy
// A bit of automatic styling
app/assets/stylesheets/custom.css.scss // Add this file, with the following line:
@import “bootstrap”; // Now you can use twitter’s bootstrap to style your app!