Categories
Uncategorized

Examples of Ruby on Rails

Basic set of examples to read if you want to learn Ruby on Rails.

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:

All posts

    <% @posts.each do |post| %>

  • <%= link_to post.title, post %>
  • <% end %>

<%= link_to "New post", new_post_path %>
<%= link_to "Back to homepage", root_path %>

In app/views/posts/show.html.erb:

<%= @post.title %>

Created: <%= @post.created_at %>

<%= link_to "Delete post", @post, :method => :delete %>

<%= @post.content %>

Comments

<% @post.comments.each do |comment| %>

<%= comment.text %>

<% end %>

<%= link_to "Back to posts", posts_path %>
In app/views/posts/new.html.erb:

New post

<%= form_for @post do |f| %>

<%= f.label :title %>
<%= f.text_field :title %>

<%= f.label :content %>
<%= f.text_area :content %>

<%= f.submit "Create post" %>

<% end %>
View layouts
In app/views/layouts/application.html.erb:



Rails example


<% if flash[:notice] %>

<%= flash[:notice] %>

<% end %>
<%= yield %>


View partials
In app/views/shared/_example_partial.html.erb:

Content of example variable: <%= example_variable %>

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