http://guides.rubyonrails.org/getting_started.html
rails new -h
see all app builder switches
app/
Contains the controllers, models, views and assets for your application. You'll focus on this folder for the remainder of this guide.
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.
doc/
In-depth documentation for your application.
Gemfile Gemfile.lock
These files allow you to specify what gem dependencies are needed for your Rails application.
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.
script/
Contains the rails script that starts your app and can contain other scripts you use to deploy or run your application.
test/
Unit tests, fixtures, and other test apparatus. These are covered in Testing Rails Applications
tmp/
Temporary files
vendor/
A place for all third-party code. In a typical Rails application, this includes Ruby Gems, the Rails source code (if you optionally install it into your project) and plugins containing additional prepackaged functionality.
rails new blog --database=mysql
this can also be used for overwriting
rake db:create
creates dev and test db's
rake -T
see all rake tasks
rails g controller home index
rm public/index.html
# config/routes.rb
Blog::Application.routes.draw do
root :to => "home#index"
rails generate scaffold Post name:string title:string content:text
rake db:migrate RAILS_ENV=production
don't need this if you're using Capistrano
Model: attr_accessible creates a whitelist for bulk updates (e.g. update_attributes)
rails c --sandbox
rolls back any db changes made
reload!
brings in changes to models into the running console
escaping in views by default <%= raw post.name %>
for unescaped output
application layout app/views/layouts/application.html.erb
partials <%= render 'form' %>
views/posts/_form.html.erb
Most Rails developers still use generators to make things like models and controllers
Model generator rails g model Comment commenter:string body:text post:references
class Comment < ActiveRecord::Base
belongs_to :post
end
# You’ll need to edit the post.rb file to add the other side of the association:
class Post < ActiveRecord::Base
# ... validation etc.
has_many :comments
# has_many :comments, :dependent => :destroy # cascade delete
end
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.string :commenter
t.text :body
t.references :post
t.timestamps
end
add_index :comments, :post_id
end
end
Model name singular, table name plural
rails g model
for help
Models inside modules rails generate model admin/account
rails g controller Comments
can be camel case or underscores, can also be modularised parent_module/controller_name
# adding a new comment to the comments associated with post
class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(params[:comment])
redirect_to post_path(@post)
end
end
module PostsHelper
def join_tags(post)
post.tags.map { |t| t.name }.join(", ")
end
end
rails generate model tag name:string post:references
accepts_nested_attributes_for :tags, :allow_destroy => :true, :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
goes in the model
see guide for forms