Rails

David Heinemeier Hansson (DHH), 37signals

The Rails philosophy includes two major guiding principles

DRY - "Don't Repeat Yourself" - suggests that writing the same code over and over again is a bad thing.

Convention Over Configuration - means that Rails makes assumptions about what you want to do and how you're going to do it, rather than requiring you to specify every little thing through endless configuration files.

Instaling Rails

Preparing environment

Installing Rails

Creating rails application

Rails new

Goto directory

Add .ruby-version and .ruby-gemset

.ruby-version .ruby-gemset

Directories structure

File/Folder Purpose
app/ Contains the controllers, models, views, helpers, mailers and assets for your application. You'll focus on this folder for the remainder of this guide.
bin/ Contains the rails script that starts your app and can contain other scripts you use to deploy or run your application.
config/ Configure your application's runtime rules, routes, database, and more. This is covered in more detail in Configuring Rails Applications
config.ru Rack configuration for Rack based servers used to start the application.
db/ Contains your current database schema, as well as the database migrations.
Gemfile
Gemfile.lock
These files allow you to specify what gem dependencies are needed for your Rails application. These files are used by the Bundler gem. For more information about Bundler, see the Bundler website
lib/ Extended modules for your application.
log/ Application log files.
public/ The only folder seen to the world as-is. Contains the static files and compiled assets.
Rakefile This file locates and loads tasks that can be run from the command line. The task definitions are defined throughout the components of Rails. Rather than changing Rakefile, you should add your own tasks by adding files to the lib/tasks directory of your application.
README.rdoc This is a brief instruction manual for your application. You should edit this file to tell others what your application does, how to set it up, and so on.
test/ Unit tests, fixtures, and other test apparatus. These are covered in Testing Rails Applications
tmp/ Temporary files (like cache, pid and session files)
vendor/ A place for all third-party code. In a typical Rails application, this includes Ruby Gems and the Rails source code (if you optionally install it into your project).

Gemfile

Database config

config/database.yml MySQL Postgresql

Creating the Database

Starting up the Web Server

Generate scaffold

Migrations

db/migrate/20131209184636_create_posts.rb

Run migration

DB schema

db/schema.rb

Routes

config/routes.rb

Models

app/models/post.rb

Controllers

app/controllers/posts_controller.rb

Listing posts

GET localhost:3000/posts
Title Text
Facebook’s New Colocation And Image Recognition Patents Tease The Future Of Sharing Facebook’s empire was built on photo tags and sharing, but it’s a grueling process many neglect. Luckily, new Facebook patents give it tech to continuously capture video whenever your camera is open, rank and surface the best images, and auto-tag them with people, places, and businesses. They tease a future where pattern, facial, and audio recognition identify what you’re seeing for easy sharing. Show Edit Destroy

New Post
GET localhost:3000/posts.json app/controllers/posts_controller.rb app/views/posts/index.html.erb app/views/posts/index.json.jbuilder

New post

GET localhost:3000/posts/new
Back
app/controllers/posts_controller.rb app/views/posts/index.html.erb app/views/posts/_form.html.erb

Create post

app/controllers/posts_controller.rb

Show post

GET localhost:3000/posts/1

Post was successfully created.

Title: Facebook’s New Colocation And Image Recognition Patents Tease The Future Of Sharing

Text: Facebook’s empire was built on photo tags and sharing, but it’s a grueling process many neglect. Luckily, new Facebook patents give it tech to continuously capture video whenever your camera is open, rank and surface the best images, and auto-tag them with people, places, and businesses. They tease a future where pattern, facial, and audio recognition identify what you’re seeing for easy sharing.

Edit | Back
GET localhost:3000/posts/1.json
{"title":"Facebook’s New Colocation And Image Recognition Patents Tease The Future Of Sharing ","text":"Facebook’s empire was built on photo tags and sharing, but it’s a grueling process many neglect. Luckily, new Facebook patents give it tech to continuously capture video whenever your camera is open, rank and surface the best images, and auto-tag them with people, places, and businesses. They tease a future where pattern, facial, and audio recognition identify what you’re seeing for easy sharing.","created_at":"2013-06-09T17:07:29.179Z","updated_at":"2013-06-09T17:07:29.179Z"}
app/controllers/posts_controller.rb app/views/posts/show.html.erb app/views/posts/show.json.jbuilder

Edit post

GET localhost:3000/posts/1/edit
Show | Back
app/controllers/posts_controller.rb app/views/posts/edit.html.erb app/views/posts/_form.html.erb

Update post

app/controllers/posts_controller.rb

Delete post

app/controllers/posts_controller.rb

Layout

app/views/layouts/application.html.erb

ApplicationController

app/controllers/application_controller.rb

Helpers

app/helpers/posts_helper.rb

Assets

app/assets/javascripts/application.js app/assets/stylesheets/application.css

/

#