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

SAVIA
SAVIASAVIA
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overviewYehuda Katz
 
Debugging on Rails. Mykhaylo Sorochan
Debugging on Rails. Mykhaylo SorochanDebugging on Rails. Mykhaylo Sorochan
Debugging on Rails. Mykhaylo Sorochan
Sphere Consulting Inc
 
A Z Introduction To Ruby On Rails
A Z Introduction To Ruby On RailsA Z Introduction To Ruby On Rails
A Z Introduction To Ruby On Railsrailsconf
 
A-Z Intro To Rails
A-Z Intro To RailsA-Z Intro To Rails
A-Z Intro To Rails
Robert Dempsey
 
Action View Form Helpers - 1, Season 2
Action View Form Helpers - 1, Season 2Action View Form Helpers - 1, Season 2
Action View Form Helpers - 1, Season 2RORLAB
 
Working With The Symfony Admin Generator
Working With The Symfony Admin GeneratorWorking With The Symfony Admin Generator
Working With The Symfony Admin Generator
John Cleveley
 
Service approach for development REST API in Symfony2
Service approach for development REST API in Symfony2Service approach for development REST API in Symfony2
Service approach for development REST API in Symfony2
Sumy PHP User Grpoup
 
OSDC 2009 Rails Turtorial
OSDC 2009 Rails TurtorialOSDC 2009 Rails Turtorial
OSDC 2009 Rails TurtorialYi-Ting Cheng
 
Rails2 Pr
Rails2 PrRails2 Pr
Rails2 Pr
xibbar
 
Service approach for development Rest API in Symfony2
Service approach for development Rest API in Symfony2Service approach for development Rest API in Symfony2
Service approach for development Rest API in Symfony2
Sumy PHP User Grpoup
 
Symfony Admin Generator
Symfony Admin GeneratorSymfony Admin Generator
Symfony Admin Generator
Nuuktal Consulting
 
Using the Features API
Using the Features APIUsing the Features API
Using the Features API
cgmonroe
 
CRUD with Dojo
CRUD with DojoCRUD with Dojo
CRUD with Dojo
Eugene Lazutkin
 
Silex Cheat Sheet
Silex Cheat SheetSilex Cheat Sheet
Silex Cheat Sheet
Andréia Bohner
 
Basic Crud In Django
Basic Crud In DjangoBasic Crud In Django
Basic Crud In Django
mcantelon
 
Curso Symfony - Clase 3
Curso Symfony - Clase 3Curso Symfony - Clase 3
Curso Symfony - Clase 3
Javier Eguiluz
 
Building a Rails Interface
Building a Rails InterfaceBuilding a Rails Interface
Building a Rails Interface
James Gray
 

What's hot (20)

SAVIA
SAVIASAVIA
SAVIA
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overview
 
Debugging on Rails. Mykhaylo Sorochan
Debugging on Rails. Mykhaylo SorochanDebugging on Rails. Mykhaylo Sorochan
Debugging on Rails. Mykhaylo Sorochan
 
A Z Introduction To Ruby On Rails
A Z Introduction To Ruby On RailsA Z Introduction To Ruby On Rails
A Z Introduction To Ruby On Rails
 
A-Z Intro To Rails
A-Z Intro To RailsA-Z Intro To Rails
A-Z Intro To Rails
 
Action View Form Helpers - 1, Season 2
Action View Form Helpers - 1, Season 2Action View Form Helpers - 1, Season 2
Action View Form Helpers - 1, Season 2
 
Working With The Symfony Admin Generator
Working With The Symfony Admin GeneratorWorking With The Symfony Admin Generator
Working With The Symfony Admin Generator
 
Service approach for development REST API in Symfony2
Service approach for development REST API in Symfony2Service approach for development REST API in Symfony2
Service approach for development REST API in Symfony2
 
OSDC 2009 Rails Turtorial
OSDC 2009 Rails TurtorialOSDC 2009 Rails Turtorial
OSDC 2009 Rails Turtorial
 
Jsf
JsfJsf
Jsf
 
Rails2 Pr
Rails2 PrRails2 Pr
Rails2 Pr
 
Service approach for development Rest API in Symfony2
Service approach for development Rest API in Symfony2Service approach for development Rest API in Symfony2
Service approach for development Rest API in Symfony2
 
Symfony Admin Generator
Symfony Admin GeneratorSymfony Admin Generator
Symfony Admin Generator
 
Using the Features API
Using the Features APIUsing the Features API
Using the Features API
 
CRUD with Dojo
CRUD with DojoCRUD with Dojo
CRUD with Dojo
 
Silex Cheat Sheet
Silex Cheat SheetSilex Cheat Sheet
Silex Cheat Sheet
 
Basic Crud In Django
Basic Crud In DjangoBasic Crud In Django
Basic Crud In Django
 
Curso Symfony - Clase 3
Curso Symfony - Clase 3Curso Symfony - Clase 3
Curso Symfony - Clase 3
 
test
testtest
test
 
Building a Rails Interface
Building a Rails InterfaceBuilding a Rails Interface
Building a Rails Interface
 

Viewers also liked

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

Viewers also liked (8)

Active Record Validations, Season 1
Active Record Validations, Season 1Active Record Validations, Season 1
Active Record Validations, Season 1
 
Asset Pipeline in Ruby on Rails
Asset Pipeline in Ruby on RailsAsset Pipeline in Ruby on Rails
Asset Pipeline in Ruby on Rails
 
Getting started with Rails (3), Season 2
Getting started with Rails (3), Season 2Getting started with Rails (3), Season 2
Getting started with Rails (3), Season 2
 
Getting Started with Rails (2)
Getting Started with Rails (2)Getting Started with Rails (2)
Getting Started with Rails (2)
 
ActiveRecord Association (1), Season 2
ActiveRecord Association (1), Season 2ActiveRecord Association (1), Season 2
ActiveRecord Association (1), Season 2
 
Active Record Query Interface (1), Season 2
Active Record Query Interface (1), Season 2Active Record Query Interface (1), Season 2
Active Record Query Interface (1), Season 2
 
Spring batch overivew
Spring batch overivewSpring batch overivew
Spring batch overivew
 
Spring Batch Workshop
Spring Batch WorkshopSpring Batch Workshop
Spring Batch Workshop
 

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

Ruby on Rails at PROMPT ISEL '11
Ruby on Rails at PROMPT ISEL '11Ruby on Rails at PROMPT ISEL '11
Ruby on Rails at PROMPT ISEL '11
Pedro Cunha
 
Introduction à Ruby
Introduction à RubyIntroduction à Ruby
Introduction à Ruby
Microsoft
 
Ruby/Rails
Ruby/RailsRuby/Rails
Ruby/Rails
rstankov
 
Django Vs Rails
Django Vs RailsDjango Vs Rails
Django Vs Rails
Sérgio Santos
 
Rails vs Web2py
Rails vs Web2pyRails vs Web2py
Rails vs Web2py
jonromero
 
More to RoC weibo
More to RoC weiboMore to RoC weibo
More to RoC weibo
shaokun
 
Pourquoi ruby et rails déchirent
Pourquoi ruby et rails déchirentPourquoi ruby et rails déchirent
Pourquoi ruby et rails déchirent
Nicolas Ledez
 
Be happy with Ruby on Rails - CEUNSP Itu
Be happy with Ruby on Rails - CEUNSP ItuBe happy with Ruby on Rails - CEUNSP Itu
Be happy with Ruby on Rails - CEUNSP Itu
Lucas Renan
 
Template rendering in rails
Template rendering in rails Template rendering in rails
Template rendering in rails
Hung Wu Lo
 
Ruby on Rails ステップアップ講座 - 大場寧子
Ruby on Rails ステップアップ講座 - 大場寧子Ruby on Rails ステップアップ講座 - 大場寧子
Ruby on Rails ステップアップ講座 - 大場寧子
Yasuko Ohba
 
RoR 101: Session 2
RoR 101: Session 2RoR 101: Session 2
RoR 101: Session 2
Rory Gianni
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the FinishYehuda Katz
 
Ruby on Rails - Introduction
Ruby on Rails - IntroductionRuby on Rails - Introduction
Ruby on Rails - Introduction
Vagmi Mudumbai
 
How To Test Everything
How To Test EverythingHow To Test Everything
How To Test Everything
noelrap
 
Why ruby
Why rubyWhy ruby
Why ruby
rstankov
 
Migration from Rails2 to Rails3
Migration from Rails2 to Rails3Migration from Rails2 to Rails3
Migration from Rails2 to Rails3
Umair Amjad
 

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

Ruby on Rails at PROMPT ISEL '11
Ruby on Rails at PROMPT ISEL '11Ruby on Rails at PROMPT ISEL '11
Ruby on Rails at PROMPT ISEL '11
 
Introduction à Ruby
Introduction à RubyIntroduction à Ruby
Introduction à Ruby
 
Ruby/Rails
Ruby/RailsRuby/Rails
Ruby/Rails
 
Django Vs Rails
Django Vs RailsDjango Vs Rails
Django Vs Rails
 
Cocoa on-rails-3rd
Cocoa on-rails-3rdCocoa on-rails-3rd
Cocoa on-rails-3rd
 
Rails vs Web2py
Rails vs Web2pyRails vs Web2py
Rails vs Web2py
 
More to RoC weibo
More to RoC weiboMore to RoC weibo
More to RoC weibo
 
The Rails Way
The Rails WayThe Rails Way
The Rails Way
 
Pourquoi ruby et rails déchirent
Pourquoi ruby et rails déchirentPourquoi ruby et rails déchirent
Pourquoi ruby et rails déchirent
 
Be happy with Ruby on Rails - CEUNSP Itu
Be happy with Ruby on Rails - CEUNSP ItuBe happy with Ruby on Rails - CEUNSP Itu
Be happy with Ruby on Rails - CEUNSP Itu
 
Template rendering in rails
Template rendering in rails Template rendering in rails
Template rendering in rails
 
Ruby on Rails ステップアップ講座 - 大場寧子
Ruby on Rails ステップアップ講座 - 大場寧子Ruby on Rails ステップアップ講座 - 大場寧子
Ruby on Rails ステップアップ講座 - 大場寧子
 
RoR 101: Session 2
RoR 101: Session 2RoR 101: Session 2
RoR 101: Session 2
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the Finish
 
Intro to Rails 4
Intro to Rails 4Intro to Rails 4
Intro to Rails 4
 
Ruby on Rails - Introduction
Ruby on Rails - IntroductionRuby on Rails - Introduction
Ruby on Rails - Introduction
 
How To Test Everything
How To Test EverythingHow To Test Everything
How To Test Everything
 
Rails 4.0
Rails 4.0Rails 4.0
Rails 4.0
 
Why ruby
Why rubyWhy ruby
Why ruby
 
Migration from Rails2 to Rails3
Migration from Rails2 to Rails3Migration from Rails2 to Rails3
Migration from Rails2 to Rails3
 

More from RORLAB

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

More from RORLAB (20)

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

Recently uploaded

The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
kimdan468
 

Recently uploaded (20)

The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
 

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

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n