SlideShare a Scribd company logo
ROR Lab. Season 2
   - The 2nd Round -



Getting Started
 with Rails (2)

      July 7, 2012

     Hyoseong Choi
       ROR Lab.
A Blog Project

                                   Post
                              ny




                                              on
                             a
                            m
separate form                                           nested form



                                                 e
                       to          form_for




                                                to
                  ne




                                                     m
                                                      an
                 o




                                                        y
      Comment                                               Tag

      form_for                                         fields_for



                                                                   ROR Lab.
Using Scaffolding
$ rails generate scaffold Post
 name:string title:string content:text




Scaffolding Generator            • MVC
                                 • asset
                                 • helper
                                 • test unit
                                 • routing
                                               ROR Lab.
Creating
a Resource w scaffolding
   $ rails g scaffold post name title content:text
       invoke active_record
       create db/migrate/20120705045702_create_posts.rb
       create app/models/post.rb
       invoke test_unit
       create     test/unit/post_test.rb
       create     test/fixtures/posts.yml
       invoke resource_route
        route resources :posts
       invoke scaffold_controller
       create app/controllers/posts_controller.rb
       invoke erb
       create     app/views/posts
       create     app/views/posts/index.html.erb
       create     app/views/posts/edit.html.erb
       create     app/views/posts/show.html.erb
       create     app/views/posts/new.html.erb
       create     app/views/posts/_form.html.erb
       invoke test_unit
       create     test/functional/posts_controller_test.rb
       invoke helper
       create     app/helpers/posts_helper.rb
       invoke      test_unit
       create       test/unit/helpers/posts_helper_test.rb
       invoke assets
       invoke coffee
       create     app/assets/javascripts/posts.js.coffee
       invoke scss
       create     app/assets/stylesheets/posts.css.scss
       invoke scss
       create app/assets/stylesheets/scaffolds.css.scss
                                                             ROR Lab.
Running
        a Migration
class CreatePosts < ActiveRecord::Migration
  def change
    create_table :posts do |t|
      t.string :name
      t.string :title
      t.text :content
 
      t.timestamps
    end
  end
end                            db/migrate/20100207214725_create_posts.rb




                                                                     ROR Lab.
Running
     a Migration
$ rake db:migrate
==  CreatePosts: migrating
=========================================
===========
-- create_table(:posts)
   -> 0.0019s
==  CreatePosts: migrated (0.0020s)




              to Where?

                                       ROR Lab.
Adding a Link
<h1>Hello, Rails!</h1>
<%= link_to "My Blog", posts_path %>
                             app/views/home/index.html.erb




 Hello, Rails!
 My Blog




                                                      ROR Lab.
The Model
class Post < ActiveRecord::Base
  attr_accessible :content, :name, :title
end
                                            app/models/post.rb




Rails                  Active
Model                  Record

                     database.yml


                                                          ROR Lab.
Adding Validations

 class Post < ActiveRecord::Base
   attr_accessible :content, :name, :title
  
   validates :name,  :presence => true
   validates :title, :presence => true,
                     :length => { :minimum => 5 }
 end                                                app/models/post.rb
                                                     app/models/post.rb




                                                                 ROR Lab.
Using the Console
 $ rails console --sandbox


 $ rails c --sandbox
 Loading development environment in sandbox (Rails
 3.2.6)
 Any modifications you make will be rolled back on exit
 1.9.3p194 :001 > Post.all
  Post Load (0.1ms) SELECT "posts".* FROM "posts"
  => []
 1.9.3p194 :002 > reload!
 Reloading...
  => true



                                                         ROR Lab.
Listing All Posts
def index
  @posts = Post.all
 
  respond_to do |format|
    format.html  # index.html.erb
    format.json  { render :json => @posts }
  end
end
                                        app/controllers/posts_controller.rb


1.9.3p194 :004 > posts = Post.all
 Post Load (0.2ms) SELECT "posts".* FROM "posts"
 => []
1.9.3p194 :005 > posts.class
 => Array
1.9.3p194 :006 > post = Post.scoped
 Post Load (0.2ms) SELECT "posts".* FROM "posts"
 => []
1.9.3p194 :007 > post.class
 => ActiveRecord::Relation
                                                                      ROR Lab.
<h1>Listing posts</h1>                                   Rails makes all of the
 
<table>                                                  instance variables from the
  <tr>                                                   action available to the view.
    <th>Name</th>
    <th>Title</th>
    <th>Content</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>
 
<% @posts.each do |post| %>
  <tr>
    <td><%= post.name %></td>
    <td><%= post.title %></td>
    <td><%= post.content %></td>
    <td><%= link_to 'Show', post %></td>
    <td><%= link_to 'Edit', edit_post_path(post) %></td>
    <td><%= link_to 'Destroy', post, :confirm => 'Are you sure?',
                                     :method => :delete %></td>
  </tr>
<% end %>
</table>
 
<br />
 
<%= link_to 'New post', new_post_path %>

                                                         app/views/posts/index.html.erb




                                                                                         ROR Lab.
The Layout
            : Containers for views

<!DOCTYPE html>
<html>
<head>
  <title>Blog</title>
  <%= stylesheet_link_tag "application" %>
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>
</head>
<body style="background: #EEEEEE;">
 
<%= yield %>
 
</body>
                              app/views/layouts/application.html.erb




                                                               ROR Lab.
Creating New Posts
 def new
   @post = Post.new
  
   respond_to do |format|
     format.html  # new.html.erb
     format.json  { render :json => @post }
   end
 end




 <h1>New post</h1>
  
 <%= render 'form' %>       # using     HTTP POST verb
  
 <%= link_to 'Back', posts_path %>
                                              app/views/posts/new.html.erb




                                                                      ROR Lab.
<%= form_for(@post) do |f| %>
  <% if @post.errors.any? %>
  <div id="errorExplanation">
    <h2><%= pluralize(@post.errors.count, "error") %> prohibited
        this post from being saved:</h2>
    <ul>
    <% @post.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
    </ul>
  </div>
  <% end %>
 
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :content %><br />
    <%= f.text_area :content %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>                                a “Partial” template
                                                  app/views/posts/_form.html.erb




                                                                            ROR Lab.
                    intelligent submit helper
ROR Lab.
“create” action




                  ROR Lab.
def create
  @post = Post.new(params[:post])
 
  respond_to do |format|
    if @post.save
      format.html  { redirect_to(@post,
                    :notice => 'Post was successfully created.') }
      format.json  { render :json => @post,
                    :status => :created, :location => @post }
    else
      format.html  { render :action => "new" }
      format.json  { render :json => @post.errors,
                    :status => :unprocessable_entity }
    end
  end
end




                                                                     ROR Lab.
Show a Post
            http://localhost:3000/posts/1


def show
  @post = Post.find(params[:id])
 
  respond_to do |format|
    format.html  # show.html.erb
    format.json  { render :json => @post }
  end
end




                                             ROR Lab.
Show a Post
               app/views/posts/show.html.erb

<p id="notice"><%= notice %></p>
 
<p>
  <b>Name:</b>
  <%= @post.name %>
</p>
 
<p>
  <b>Title:</b>
  <%= @post.title %>
</p>
 
<p>
  <b>Content:</b>
  <%= @post.content %>
</p>
  
<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %>



                                                 ROR Lab.
Editing Posts
def edit
  @post = Post.find(params[:id])




<h1>Editing post</h1>
 
<%= render 'form' %>      # using   HTTP PUT verb
 
<%= link_to 'Show', @post %> |
<%= link_to 'Back', posts_path %>
                                      app/views/posts/edit.html.erb




                                                               ROR Lab.
Editing Posts
def update
  @post = Post.find(params[:id])
 
  respond_to do |format|
    if @post.update_attributes(params[:post])
      format.html  { redirect_to(@post,
                    :notice => 'Post was successfully updated.') }
      format.json  { head :no_content }
    else
      format.html  { render :action => "edit" }
      format.json  { render :json => @post.errors,
                    :status => :unprocessable_entity }
    end
  end
end




                                                                     ROR Lab.
Destroying a Post
 def destroy
   @post = Post.find(params[:id])
   @post.destroy
  
   respond_to do |format|
     format.html { redirect_to posts_url }
     format.json { head :no_content }
   end




                                             ROR Lab.
Git

• Linus Tobalds, 1991
• for source code version control
• local repository

                                    ROR Lab.
Gitosis
• Remote repository
• git clone git://eagain.net/gitosis.git
• git clone git://github.com/res0nat0r/
  gitosis.git
• Ubuntu 11.10 -> gitosis package
• Ubuntu 12.04 -> not any more but gitolite
                                           ROR Lab.
Gitosis
• Remote repository
• git clone git://eagain.net/gitosis.git
• git clone git://github.com/res0nat0r/
  gitosis.git
• Ubuntu 11.10 -> gitosis package
• Ubuntu 12.04 -> not any more but gitolite
                                           ROR Lab.
Local Machine   Git Repository Sever
                   git@gitserver:project.git




                        gitosis
                        gitolite
                       gitorious
$ git add *
$ git commit
$ git push                Origin


                                   ROR Lab.
감사합니다.

More Related Content

What's hot (20)

PDF
SAVIA
Julián Pérez
 
PDF
Rails 3 overview
Yehuda Katz
 
PDF
Debugging on Rails. Mykhaylo Sorochan
Sphere Consulting Inc
 
PDF
A Z Introduction To Ruby On Rails
railsconf
 
KEY
A-Z Intro To Rails
Robert Dempsey
 
PDF
Action View Form Helpers - 1, Season 2
RORLAB
 
PDF
Working With The Symfony Admin Generator
John Cleveley
 
PPTX
Service approach for development REST API in Symfony2
Sumy PHP User Grpoup
 
PDF
OSDC 2009 Rails Turtorial
Yi-Ting Cheng
 
PDF
Rails2 Pr
xibbar
 
PPTX
Service approach for development Rest API in Symfony2
Sumy PHP User Grpoup
 
PDF
Symfony Admin Generator
Nuuktal Consulting
 
PPTX
Using the Features API
cgmonroe
 
PPT
CRUD with Dojo
Eugene Lazutkin
 
PDF
Silex Cheat Sheet
Andréia Bohner
 
PDF
Basic Crud In Django
mcantelon
 
PDF
Curso Symfony - Clase 3
Javier Eguiluz
 
KEY
Building a Rails Interface
James Gray
 
Rails 3 overview
Yehuda Katz
 
Debugging on Rails. Mykhaylo Sorochan
Sphere Consulting Inc
 
A Z Introduction To Ruby On Rails
railsconf
 
A-Z Intro To Rails
Robert Dempsey
 
Action View Form Helpers - 1, Season 2
RORLAB
 
Working With The Symfony Admin Generator
John Cleveley
 
Service approach for development REST API in Symfony2
Sumy PHP User Grpoup
 
OSDC 2009 Rails Turtorial
Yi-Ting Cheng
 
Rails2 Pr
xibbar
 
Service approach for development Rest API in Symfony2
Sumy PHP User Grpoup
 
Symfony Admin Generator
Nuuktal Consulting
 
Using the Features API
cgmonroe
 
CRUD with Dojo
Eugene Lazutkin
 
Silex Cheat Sheet
Andréia Bohner
 
Basic Crud In Django
mcantelon
 
Curso Symfony - Clase 3
Javier Eguiluz
 
Building a Rails Interface
James Gray
 

Viewers also liked (8)

KEY
Active Record Validations, Season 1
RORLAB
 
PDF
Asset Pipeline in Ruby on Rails
RORLAB
 
KEY
Getting started with Rails (3), Season 2
RORLAB
 
PDF
Getting Started with Rails (2)
RORLAB
 
KEY
ActiveRecord Association (1), Season 2
RORLAB
 
KEY
Active Record Query Interface (1), Season 2
RORLAB
 
PDF
Spring batch overivew
Chanyeong Choi
 
PDF
Spring Batch Workshop
lyonjug
 
Active Record Validations, Season 1
RORLAB
 
Asset Pipeline in Ruby on Rails
RORLAB
 
Getting started with Rails (3), Season 2
RORLAB
 
Getting Started with Rails (2)
RORLAB
 
ActiveRecord Association (1), Season 2
RORLAB
 
Active Record Query Interface (1), Season 2
RORLAB
 
Spring batch overivew
Chanyeong Choi
 
Spring Batch Workshop
lyonjug
 
Ad

Similar to Getting started with Rails (2), Season 2 (20)

PDF
Ruby on Rails at PROMPT ISEL '11
Pedro Cunha
 
KEY
Getting started with Rails (4), Season 2
RORLAB
 
ZIP
Barcamp Auckland Rails3 presentation
Sociable
 
PDF
Ruby On Rails Introduction
Thomas Fuchs
 
KEY
Rails3ハンズオン資料
Shinsaku Chikura
 
PDF
Be happy with Ruby on Rails - CEUNSP Itu
Lucas Renan
 
PDF
浜松Rails3道場 其の四 View編
Masakuni Kato
 
KEY
深入淺出RoR
Eric Lee
 
PDF
Curso rails
Icalia Labs
 
PDF
Rails 3: Dashing to the Finish
Yehuda Katz
 
ZIP
Rails 3 (beta) Roundup
Wayne Carter
 
ODP
Como programar un blog REST
Javier Vidal
 
KEY
Routing 1, Season 1
RORLAB
 
PDF
HES2011 - joernchen - Ruby on Rails from a Code Auditor Perspective
Hackito Ergo Sum
 
PPTX
Learning to code for startup mvp session 3
Henry S
 
PPT
Introduction to Ruby on Rails
Manoj Kumar
 
PPT
Rail3 intro 29th_sep_surendran
SPRITLE SOFTWARE PRIVATE LIMIT ED
 
PDF
Ruby on Rails - Introduction
Vagmi Mudumbai
 
KEY
More to RoC weibo
shaokun
 
PDF
Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009
Yasuko Ohba
 
Ruby on Rails at PROMPT ISEL '11
Pedro Cunha
 
Getting started with Rails (4), Season 2
RORLAB
 
Barcamp Auckland Rails3 presentation
Sociable
 
Ruby On Rails Introduction
Thomas Fuchs
 
Rails3ハンズオン資料
Shinsaku Chikura
 
Be happy with Ruby on Rails - CEUNSP Itu
Lucas Renan
 
浜松Rails3道場 其の四 View編
Masakuni Kato
 
深入淺出RoR
Eric Lee
 
Curso rails
Icalia Labs
 
Rails 3: Dashing to the Finish
Yehuda Katz
 
Rails 3 (beta) Roundup
Wayne Carter
 
Como programar un blog REST
Javier Vidal
 
Routing 1, Season 1
RORLAB
 
HES2011 - joernchen - Ruby on Rails from a Code Auditor Perspective
Hackito Ergo Sum
 
Learning to code for startup mvp session 3
Henry S
 
Introduction to Ruby on Rails
Manoj Kumar
 
Rail3 intro 29th_sep_surendran
SPRITLE SOFTWARE PRIVATE LIMIT ED
 
Ruby on Rails - Introduction
Vagmi Mudumbai
 
More to RoC weibo
shaokun
 
Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009
Yasuko Ohba
 
Ad

More from RORLAB (20)

PDF
Getting Started with Rails (4)
RORLAB
 
PDF
Getting Started with Rails (3)
RORLAB
 
PDF
Getting Started with Rails (1)
RORLAB
 
PDF
Self join in active record association
RORLAB
 
PDF
레일스가이드 한글번역 공개프로젝트 RORLabGuides 소개
RORLAB
 
PDF
Active Support Core Extension (3)
RORLAB
 
PDF
Active Support Core Extension (2)
RORLAB
 
PDF
Active Support Core Extensions (1)
RORLAB
 
PDF
Action View Form Helpers - 2, Season 2
RORLAB
 
PDF
ActiveRecord Query Interface (2), Season 2
RORLAB
 
KEY
Active Record Association (2), Season 2
RORLAB
 
KEY
ActiveRecord Callbacks & Observers, Season 2
RORLAB
 
KEY
ActiveRecord Validations, Season 2
RORLAB
 
KEY
Rails Database Migration, Season 2
RORLAB
 
KEY
Getting started with Rails (1), Season 2
RORLAB
 
KEY
Routing 2, Season 1
RORLAB
 
KEY
Action Controller Overview, Season 1
RORLAB
 
KEY
Active Record Form Helpers, Season 1
RORLAB
 
KEY
Active Record Query Interface (2), Season 1
RORLAB
 
PDF
ActiveRecord Query Interface (1), Season 1
RORLAB
 
Getting Started with Rails (4)
RORLAB
 
Getting Started with Rails (3)
RORLAB
 
Getting Started with Rails (1)
RORLAB
 
Self join in active record association
RORLAB
 
레일스가이드 한글번역 공개프로젝트 RORLabGuides 소개
RORLAB
 
Active Support Core Extension (3)
RORLAB
 
Active Support Core Extension (2)
RORLAB
 
Active Support Core Extensions (1)
RORLAB
 
Action View Form Helpers - 2, Season 2
RORLAB
 
ActiveRecord Query Interface (2), Season 2
RORLAB
 
Active Record Association (2), Season 2
RORLAB
 
ActiveRecord Callbacks & Observers, Season 2
RORLAB
 
ActiveRecord Validations, Season 2
RORLAB
 
Rails Database Migration, Season 2
RORLAB
 
Getting started with Rails (1), Season 2
RORLAB
 
Routing 2, Season 1
RORLAB
 
Action Controller Overview, Season 1
RORLAB
 
Active Record Form Helpers, Season 1
RORLAB
 
Active Record Query Interface (2), Season 1
RORLAB
 
ActiveRecord Query Interface (1), Season 1
RORLAB
 

Recently uploaded (20)

PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PPTX
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
PPTX
Explorando Recursos do Summer '25: Dicas Essenciais - 02
Mauricio Alexandre Silva
 
PPTX
How to Create Rental Orders in Odoo 18 Rental
Celine George
 
PPTX
Nutri-QUIZ-Bee-Elementary.pptx...................
ferdinandsanbuenaven
 
PPSX
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
PPTX
Gall bladder, Small intestine and Large intestine.pptx
rekhapositivity
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PPTX
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
PPSX
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
PPTX
SCHOOL-BASED SEXUAL HARASSMENT PREVENTION AND RESPONSE WORKSHOP
komlalokoe
 
PDF
The-Beginnings-of-Indian-Civilisation.pdf/6th class new ncert social/by k san...
Sandeep Swamy
 
PPTX
HEAD INJURY IN CHILDREN: NURSING MANAGEMENGT.pptx
PRADEEP ABOTHU
 
PDF
Zoology (Animal Physiology) practical Manual
raviralanaresh2
 
PPTX
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
Capitol Doctoral Presentation -July 2025.pptx
CapitolTechU
 
PPTX
How to Define Translation to Custom Module And Add a new language in Odoo 18
Celine George
 
PDF
1, 2, 3… E MAIS UM CICLO CHEGA AO FIM!.pdf
Colégio Santa Teresinha
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
Explorando Recursos do Summer '25: Dicas Essenciais - 02
Mauricio Alexandre Silva
 
How to Create Rental Orders in Odoo 18 Rental
Celine George
 
Nutri-QUIZ-Bee-Elementary.pptx...................
ferdinandsanbuenaven
 
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
Gall bladder, Small intestine and Large intestine.pptx
rekhapositivity
 
community health nursing question paper 2.pdf
Prince kumar
 
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
SCHOOL-BASED SEXUAL HARASSMENT PREVENTION AND RESPONSE WORKSHOP
komlalokoe
 
The-Beginnings-of-Indian-Civilisation.pdf/6th class new ncert social/by k san...
Sandeep Swamy
 
HEAD INJURY IN CHILDREN: NURSING MANAGEMENGT.pptx
PRADEEP ABOTHU
 
Zoology (Animal Physiology) practical Manual
raviralanaresh2
 
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Capitol Doctoral Presentation -July 2025.pptx
CapitolTechU
 
How to Define Translation to Custom Module And Add a new language in Odoo 18
Celine George
 
1, 2, 3… E MAIS UM CICLO CHEGA AO FIM!.pdf
Colégio Santa Teresinha
 

Getting started with Rails (2), Season 2

  • 1. ROR Lab. Season 2 - The 2nd Round - Getting Started with Rails (2) July 7, 2012 Hyoseong Choi ROR Lab.
  • 2. A Blog Project Post ny on a m separate form nested form e to form_for to ne m an o y Comment Tag form_for fields_for ROR Lab.
  • 3. Using Scaffolding $ rails generate scaffold Post name:string title:string content:text Scaffolding Generator • MVC • asset • helper • test unit • routing ROR Lab.
  • 4. Creating a Resource w scaffolding $ rails g scaffold post name title content:text invoke active_record create db/migrate/20120705045702_create_posts.rb create app/models/post.rb invoke test_unit create test/unit/post_test.rb create test/fixtures/posts.yml invoke resource_route route resources :posts invoke scaffold_controller create app/controllers/posts_controller.rb invoke erb create app/views/posts create app/views/posts/index.html.erb create app/views/posts/edit.html.erb create app/views/posts/show.html.erb create app/views/posts/new.html.erb create app/views/posts/_form.html.erb invoke test_unit create test/functional/posts_controller_test.rb invoke helper create app/helpers/posts_helper.rb invoke test_unit create test/unit/helpers/posts_helper_test.rb invoke assets invoke coffee create app/assets/javascripts/posts.js.coffee invoke scss create app/assets/stylesheets/posts.css.scss invoke scss create app/assets/stylesheets/scaffolds.css.scss ROR Lab.
  • 5. Running a Migration class CreatePosts < ActiveRecord::Migration   def change     create_table :posts do |t|       t.string :name       t.string :title       t.text :content         t.timestamps     end   end end db/migrate/20100207214725_create_posts.rb ROR Lab.
  • 6. Running a Migration $ rake db:migrate ==  CreatePosts: migrating ========================================= =========== -- create_table(:posts)    -> 0.0019s ==  CreatePosts: migrated (0.0020s) to Where? ROR Lab.
  • 7. Adding a Link <h1>Hello, Rails!</h1> <%= link_to "My Blog", posts_path %> app/views/home/index.html.erb Hello, Rails! My Blog ROR Lab.
  • 8. The Model class Post < ActiveRecord::Base   attr_accessible :content, :name, :title end app/models/post.rb Rails Active Model Record database.yml ROR Lab.
  • 9. Adding Validations class Post < ActiveRecord::Base   attr_accessible :content, :name, :title     validates :name,  :presence => true   validates :title, :presence => true,                     :length => { :minimum => 5 } end app/models/post.rb app/models/post.rb ROR Lab.
  • 10. Using the Console $ rails console --sandbox $ rails c --sandbox Loading development environment in sandbox (Rails 3.2.6) Any modifications you make will be rolled back on exit 1.9.3p194 :001 > Post.all Post Load (0.1ms) SELECT "posts".* FROM "posts" => [] 1.9.3p194 :002 > reload! Reloading... => true ROR Lab.
  • 11. Listing All Posts def index   @posts = Post.all     respond_to do |format|     format.html  # index.html.erb     format.json  { render :json => @posts }   end end app/controllers/posts_controller.rb 1.9.3p194 :004 > posts = Post.all Post Load (0.2ms) SELECT "posts".* FROM "posts" => [] 1.9.3p194 :005 > posts.class => Array 1.9.3p194 :006 > post = Post.scoped Post Load (0.2ms) SELECT "posts".* FROM "posts" => [] 1.9.3p194 :007 > post.class => ActiveRecord::Relation ROR Lab.
  • 12. <h1>Listing posts</h1> Rails makes all of the   <table> instance variables from the   <tr> action available to the view.     <th>Name</th>     <th>Title</th>     <th>Content</th>     <th></th>     <th></th>     <th></th>   </tr>   <% @posts.each do |post| %>   <tr>     <td><%= post.name %></td>     <td><%= post.title %></td>     <td><%= post.content %></td>     <td><%= link_to 'Show', post %></td>     <td><%= link_to 'Edit', edit_post_path(post) %></td>     <td><%= link_to 'Destroy', post, :confirm => 'Are you sure?',                                      :method => :delete %></td>   </tr> <% end %> </table>   <br />   <%= link_to 'New post', new_post_path %> app/views/posts/index.html.erb ROR Lab.
  • 13. The Layout : Containers for views <!DOCTYPE html> <html> <head>   <title>Blog</title>   <%= stylesheet_link_tag "application" %>   <%= javascript_include_tag "application" %>   <%= csrf_meta_tags %> </head> <body style="background: #EEEEEE;">   <%= yield %>   </body> app/views/layouts/application.html.erb ROR Lab.
  • 14. Creating New Posts def new   @post = Post.new     respond_to do |format|     format.html  # new.html.erb     format.json  { render :json => @post }   end end <h1>New post</h1>   <%= render 'form' %> # using HTTP POST verb   <%= link_to 'Back', posts_path %> app/views/posts/new.html.erb ROR Lab.
  • 15. <%= form_for(@post) do |f| %>   <% if @post.errors.any? %>   <div id="errorExplanation">     <h2><%= pluralize(@post.errors.count, "error") %> prohibited         this post from being saved:</h2>     <ul>     <% @post.errors.full_messages.each do |msg| %>       <li><%= msg %></li>     <% end %>     </ul>   </div>   <% end %>     <div class="field">     <%= f.label :name %><br />     <%= f.text_field :name %>   </div>   <div class="field">     <%= f.label :title %><br />     <%= f.text_field :title %>   </div>   <div class="field">     <%= f.label :content %><br />     <%= f.text_area :content %>   </div>   <div class="actions">     <%= f.submit %>   </div> <% end %> a “Partial” template app/views/posts/_form.html.erb ROR Lab. intelligent submit helper
  • 18. def create   @post = Post.new(params[:post])     respond_to do |format|     if @post.save       format.html  { redirect_to(@post,                     :notice => 'Post was successfully created.') }       format.json  { render :json => @post,                     :status => :created, :location => @post }     else       format.html  { render :action => "new" }       format.json  { render :json => @post.errors,                     :status => :unprocessable_entity }     end   end end ROR Lab.
  • 19. Show a Post http://localhost:3000/posts/1 def show   @post = Post.find(params[:id])     respond_to do |format|     format.html  # show.html.erb     format.json  { render :json => @post }   end end ROR Lab.
  • 20. Show a Post app/views/posts/show.html.erb <p id="notice"><%= notice %></p>   <p>   <b>Name:</b>   <%= @post.name %> </p>   <p>   <b>Title:</b>   <%= @post.title %> </p>   <p>   <b>Content:</b>   <%= @post.content %> </p>    <%= link_to 'Edit', edit_post_path(@post) %> | <%= link_to 'Back', posts_path %> ROR Lab.
  • 21. Editing Posts def edit   @post = Post.find(params[:id]) <h1>Editing post</h1>   <%= render 'form' %> # using HTTP PUT verb   <%= link_to 'Show', @post %> | <%= link_to 'Back', posts_path %> app/views/posts/edit.html.erb ROR Lab.
  • 22. Editing Posts def update   @post = Post.find(params[:id])     respond_to do |format|     if @post.update_attributes(params[:post])       format.html  { redirect_to(@post,                     :notice => 'Post was successfully updated.') }       format.json  { head :no_content }     else       format.html  { render :action => "edit" }       format.json  { render :json => @post.errors,                     :status => :unprocessable_entity }     end   end end ROR Lab.
  • 23. Destroying a Post def destroy   @post = Post.find(params[:id])   @post.destroy     respond_to do |format|     format.html { redirect_to posts_url }     format.json { head :no_content }   end ROR Lab.
  • 24. Git • Linus Tobalds, 1991 • for source code version control • local repository ROR Lab.
  • 25. Gitosis • Remote repository • git clone git://eagain.net/gitosis.git • git clone git://github.com/res0nat0r/ gitosis.git • Ubuntu 11.10 -> gitosis package • Ubuntu 12.04 -> not any more but gitolite ROR Lab.
  • 26. Gitosis • Remote repository • git clone git://eagain.net/gitosis.git • git clone git://github.com/res0nat0r/ gitosis.git • Ubuntu 11.10 -> gitosis package • Ubuntu 12.04 -> not any more but gitolite ROR Lab.
  • 27. Local Machine Git Repository Sever git@gitserver:project.git gitosis gitolite gitorious $ git add * $ git commit $ git push Origin ROR Lab.
  • 29.   ROR Lab.

Editor's Notes