When I want to learn a new programming language or framework, I usually first try to find some examples to look at. Many times I have found it hard to find a single page with some good examples on how a language works. Not a tutorial, just examples. Here is a basic set of examples I would find nice to read if I wanted to learn about the Ruby on Rails web application development framework. Find more resources for learning the framework at the bottom of the post.
New Rails project
In the shell:
$ rails new example_project $ cd example_project
Models
In app/models/post.rb:
class Post < ActiveRecord::Base has_many :comments, :dependent => :destroy # post.comments with autodelete validates_presence_of :title, :content end
In app/models/comment.rb:
class Comment < ActiveRecord::Base belongs_to :post # comment.post validates_presence_of :text end
Controllers
In app/controllers/posts_controller.rb:
class PostsController < ApplicationController def index @posts = Post.all end def show @post = Post.find(params[:id]) end def new @post = Post.new end def create @post = Post.new(params[:post]) if @post.save redirect_to @post, :notice => "The post was created successfully." else render :new end end def edit @post = Post.find(params[:id]) end def update @post = Post.find(params[:id]) if @post.update_attributes(params[:post]) redirect_to @post, :notice => "The post was updated successfully." else render :edit end end def destroy @post = Post.find(params[:id]) @post.destroy redirect_to posts_path, :notice => "The post was deleted successfully." end end
Views
In app/views/posts/index.html.erb:
<h1>All posts</h1> <ul> <% @posts.each do |post| %> <li><%= link_to post.title, post %></li> <% end %> </ul> <p> <%= link_to "New post", new_post_path %> <%= link_to "Back to homepage", root_path %> </p>
In app/views/posts/show.html.erb:
<h1><%= @post.title %></h1> <p> Created: <%= @post.created_at %> </p> <p> <%= link_to "Delete post", @post, :method => :delete %> </p> <p> <%= @post.content %> </p> <h2>Comments</h2> <% @post.comments.each do |comment| %> <p> <%= comment.text %> </p> <% end %> <%= link_to "Back to posts", posts_path %>
In app/views/posts/new.html.erb:
<h1>New post</h1> <%= form_for @post do |f| %> <p> <%= f.label :title %><br /> <%= f.text_field :title %> </p> <p> <%= f.label :content %><br /> <%= f.text_area :content %> </p> <p> <%= f.submit "Create post" %> </p> <% end %>
View layouts
In app/views/layouts/application.html.erb:
<html> <head> <title>Rails example</title> </head> <body> <% if flash[:notice] %> <p><%= flash[:notice] %></p> <% end %> <%= yield %> </body> </html>
View partials
In app/views/shared/_example_partial.html.erb:
<p> Content of example variable: <%= example_variable %> </p>
In another view:
<%= render "shared/example_partial", :example_variable => "Example content" %>
Rendering other formats
In app/controllers/posts_controller.rb:
class PostsController < ApplicationController def index @posts = Post.all respond_to do |format| format.html # index.html.erb format.json { render :json => @posts } end end end
In the browser:
http://localhost:3000/posts.json
Database migrations
In the shell:
$ rails generate model post title:string content:text
In db/migrate/20121228215855_create_posts.rb:
class CreatePosts < ActiveRecord::Migration def change create_table :posts do |t| t.string :title t.text :content t.timestamps end end end
In the shell:
$ rails generate model comment post_id:integer text:text
In db/migrate/20121228220327_create_comments.rb:
class CreateComments < ActiveRecord::Migration def change create_table :comments do |t| t.integer :post_id t.text :text t.timestamps end add_index :comments, :post_id end end
Then, in the shell:
$ rake db:migrate
Routing
In config/routes.rb:
ExampleProject::Application.routes.draw do resources :posts do # /posts, /posts/123, etc. => PostsController resources :comments # /posts/123/comments, /posts/123/comments/345, etc. => CommentsController end match "contact" => "home#contact" # /contact => HomeController#contact root :to => "home#index" # / => HomeController#index end
Plugins
In Gemfile:
# To use jQuery gem 'jquery-rails' # To use Jbuilder templates for JSON gem 'jbuilder' # Deploy with Capistrano gem 'capistrano'
In the shell:
$ bundle install
Rails development server
In the shell:
$ rails server
To see posts:
http://localhost:3000/posts
To see a single post:
http://localhost:3000/posts/123
To edit a post:
http://localhost:3000/posts/123/edit
See also
- What is Ruby on Rails (an introduction) – a general introduction to Ruby on Rails
- Ruby on Rails official site – the official site with info on Ruby on Rails
- Ruby on Rails official guides – introductory guides to get started
- RailsCasts – a lot of useful screencasts about Ruby on Rails
- TryRuby – an interactive shell where you can try the Ruby language
- Ruby on Wikipedia – Wikipedia article on the Ruby language