SlideShare ist ein Scribd-Unternehmen logo
1 von 28
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.
감사합니다.

Weitere ähnliche Inhalte

Was ist angesagt?

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 SorochanSphere 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
 
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 GeneratorJohn 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 Symfony2Sumy 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 Prxibbar
 
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 Symfony2Sumy PHP User Grpoup
 
Using the Features API
Using the Features APIUsing the Features API
Using the Features APIcgmonroe
 
Basic Crud In Django
Basic Crud In DjangoBasic Crud In Django
Basic Crud In Djangomcantelon
 
Curso Symfony - Clase 3
Curso Symfony - Clase 3Curso Symfony - Clase 3
Curso Symfony - Clase 3Javier Eguiluz
 
Building a Rails Interface
Building a Rails InterfaceBuilding a Rails Interface
Building a Rails InterfaceJames Gray
 

Was ist angesagt? (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
 

Andere mochten auch

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 RailsRORLAB
 
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 2RORLAB
 
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 2RORLAB
 
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 2RORLAB
 
Spring Batch Workshop
Spring Batch WorkshopSpring Batch Workshop
Spring Batch Workshoplyonjug
 

Andere mochten auch (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
 

Ähnlich wie 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 '11Pedro Cunha
 
Introduction à Ruby
Introduction à RubyIntroduction à Ruby
Introduction à RubyMicrosoft
 
Ruby/Rails
Ruby/RailsRuby/Rails
Ruby/Railsrstankov
 
Rails vs Web2py
Rails vs Web2pyRails vs Web2py
Rails vs Web2pyjonromero
 
More to RoC weibo
More to RoC weiboMore to RoC weibo
More to RoC weiboshaokun
 
Pourquoi ruby et rails déchirent
Pourquoi ruby et rails déchirentPourquoi ruby et rails déchirent
Pourquoi ruby et rails déchirentNicolas 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 ItuLucas 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 2Rory 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 - IntroductionVagmi Mudumbai
 
How To Test Everything
How To Test EverythingHow To Test Everything
How To Test Everythingnoelrap
 
Migration from Rails2 to Rails3
Migration from Rails2 to Rails3Migration from Rails2 to Rails3
Migration from Rails2 to Rails3Umair Amjad
 

Ähnlich wie 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
 

Mehr von 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 associationRORLAB
 
레일스가이드 한글번역 공개프로젝트 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 2RORLAB
 
ActiveRecord Callbacks & Observers, Season 2
ActiveRecord Callbacks & Observers, Season 2ActiveRecord Callbacks & Observers, Season 2
ActiveRecord Callbacks & Observers, Season 2RORLAB
 
ActiveRecord Validations, Season 2
ActiveRecord Validations, Season 2ActiveRecord Validations, Season 2
ActiveRecord Validations, Season 2RORLAB
 
Rails Database Migration, Season 2
Rails Database Migration, Season 2Rails Database Migration, Season 2
Rails Database Migration, Season 2RORLAB
 
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 2RORLAB
 
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 2RORLAB
 
Routing 2, Season 1
Routing 2, Season 1Routing 2, Season 1
Routing 2, Season 1RORLAB
 
Routing 1, Season 1
Routing 1, Season 1Routing 1, Season 1
Routing 1, Season 1RORLAB
 
Action Controller Overview, Season 1
Action Controller Overview, Season 1Action Controller Overview, Season 1
Action Controller Overview, Season 1RORLAB
 
Active Record Form Helpers, Season 1
Active Record Form Helpers, Season 1Active Record Form Helpers, Season 1
Active Record Form Helpers, Season 1RORLAB
 

Mehr von 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
 

Kürzlich hochgeladen

EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...
EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...
EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...liera silvan
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 

Kürzlich hochgeladen (20)

INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...
EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...
EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 

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.

Hinweis der Redaktion

  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