SlideShare a Scribd company logo
1 of 48
ROR Lab. Season 2
   - The 3rd Round -



Getting Started
 with Rails (3)

      July 21, 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.
Adding
a Second Model
  Class            Database


Comment     ORM

                  comments
 singular
                    plural

                         ROR Lab.
Adding
a Second Model
Model Class            Database Table


Comment       Active
              Record
                       comments

  object                  record
attributes                 fields
                                ROR Lab.
Generating
       a Model
$ rails generate model Comment
               commenter:string
               body:text
               post:references


         generate scaffold
         generate model
         generate controller
         generate migration

                                  ROR Lab.
Generating
           a Model
 $ rails generate model Comment
                commenter:string
                body:text


                                        Migration file
        Model Class        : db/migrate/xxxx_create_comment.rb
 : app/models/comment.rb

   Comment                         Comments

belongs_to :post               post_id :integer
 post:references
                                                   ROR Lab.
Generating
                   a Model
class Comment < ActiveRecord::Base
  belongs_to :post
end
                                                 Model class file
                              @comment.post

class CreateComments < ActiveRecord::Migration
  def change
    create_table :comments do |t|
      t.string :commenter
      t.text :body
      t.references :post
 
      t.timestamps                               Migration file
    end
 
    add_index :comments, :post_id
  end
end
                          $ rake db:migrate

                                                         ROR Lab.
Associating
            Models
  Parent Model       Relation          Child Model

                     has_many
     Post                           Comment

                     belongs_to
app/models/post.rb                app/models/comment.rb



                                               ROR Lab.
Associating
        Models
 class Post < ActiveRecord::Base
   attr_accessible :content, :name, :title
  
   validates :name,  :presence => true
   validates :title, :presence => true,
                     :length => { :minimum => 5 }
  
   has_many :comments
 end


                                        app/models/post.rb
Automatic behavior :
        @post.comments
                                                             ROR Lab.
Adding a Route
config/routes.rb

resources :posts do
  resources :comments
end




                        ROR Lab.
Generating
    a Controller
$ rails generate controller Comments




                                       ROR Lab.
app/views/posts/show.html.erb
                                                        A post
<p id="notice"><%= notice %></p>
 
<p>
  <b>Name:</b>
  <%= @post.name %>
</p>
 
<p>
  <b>Title:</b>                                         comments:
  <%= @post.title %>
</p>
 
<p>
  <b>Content:</b>
  <%= @post.content %>
</p>                                                                submit
 
<h2>Add a comment:</h2>
<%= form_for([@post, @post.comments.build]) do |f| %>
  <div class="field">
    <%= f.label :commenter %><br />
    <%= f.text_field :commenter %>
  </div>
  <div class="field">
    <%= f.label :body %><br />
    <%= f.text_area :body %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
 
<%= link_to 'Edit Post', edit_post_path(@post) %> |
<%= link_to 'Back to Posts', posts_path %> |
                                                          ROR Lab.
app/views/posts/show.html.erb
                                                        A post
<p id="notice"><%= notice %></p>
 
<p>
  <b>Name:</b>
  <%= @post.name %>
</p>
 
<p>
  <b>Title:</b>                                         comments:
  <%= @post.title %>
</p>
 
<p>
  <b>Content:</b>
  <%= @post.content %>
</p>                                                                submit
 
<h2>Add a comment:</h2>
<%= form_for([@post, @post.comments.build]) do |f| %>
  <div class="field">
    <%= f.label :commenter %><br />
    <%= f.text_field :commenter %>
  </div>
  <div class="field">
    <%= f.label :body %><br />
    <%= f.text_area :body %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
 
<%= link_to 'Edit Post', edit_post_path(@post) %> |
<%= link_to 'Back to Posts', posts_path %> |
                                                          ROR Lab.
app/views/posts/show.html.erb
                                                        A post
<p id="notice"><%= notice %></p>
 
<p>
  <b>Name:</b>
  <%= @post.name %>
</p>
 
<p>
  <b>Title:</b>                                         comments:
  <%= @post.title %>
</p>
 
<p>
  <b>Content:</b>
  <%= @post.content %>
</p>                                                                submit
 
<h2>Add a comment:</h2>
<%= form_for([@post, @post.comments.build]) do |f| %>
  <div class="field">
    <%= f.label :commenter %><br />
    <%= f.text_field :commenter %>
  </div>
  <div class="field">
    <%= f.label :body %><br />
    <%= f.text_area :body %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
 
<%= link_to 'Edit Post', edit_post_path(@post) %> |
<%= link_to 'Back to Posts', posts_path %> |
                                                          ROR Lab.
Creating
       a Comment
 $ rails generate controller Comments
               create destroy


/app/controllers/comments_controller.rb

 class CommentsController < ApplicationController
   def create
     @post = Post.find(params[:post_id])
     @comment = @post.comments.create(params[:comment])
     redirect_to post_path(@post)
   end
 end




                                                          ROR Lab.
Creating
              a Comment
/app/views/posts/show.html.erb
                                          A post
 <h2>Comments</h2>
 <% @post.comments.each do |comment| %>
   <p>
     <b>Commenter:</b>
     <%= comment.commenter %>
   </p>
  
   <p>
                                          comments:
     <b>Comment:</b>
     <%= comment.body %>
   </p>
 <% end %>
                                                      submit




                                              ROR Lab.
Creating
              a Comment
/app/views/posts/show.html.erb
                                          A post
 <h2>Comments</h2>
 <% @post.comments.each do |comment| %>
   <p>
     <b>Commenter:</b>
     <%= comment.commenter %>
   </p>
  
   <p>
                                          comments:
     <b>Comment:</b>
     <%= comment.body %>
   </p>
 <% end %>
                                                      submit




                                              ROR Lab.
Refactoring


• Getting long and awkward
• Using “partials” to clean up


                                 ROR Lab.
Refactoring
Rendering Partial Collections
/app/views/posts/show.html.erb
                                          A post
 <h2>Comments</h2>
 <% @post.comments.each do |comment| %>
   <p>
     <b>Commenter:</b>
     <%= comment.commenter %>             comments:
   </p>
  
   <p>
     <b>Comment:</b>
     <%= comment.body %>
   </p>
 <% end %>
                                                      submit




                                              ROR Lab.
Refactoring
Rendering Partial Collections
/app/views/posts/show.html.erb
                                          A post
 <h2>Comments</h2>
 <% @post.comments.each do |comment| %>
   <p>
     <b>Commenter:</b>
     <%= comment.commenter %>             comments:
   </p>
  
   <p>
     <b>Comment:</b>
     <%= comment.body %>
   </p>
 <% end %>
                                                      submit




                                              ROR Lab.
Refactoring
Rendering Partial Collections
/app/views/posts/show.html.erb
                                          A post
 <h2>Comments</h2>
 <% @post.comments.each do |comment| %>
   <p>
     <b>Commenter:</b>
     <%= comment.commenter %>             comments:
   </p>
  
   <p>
     <b>Comment:</b>
     <%= comment.body %>
   </p>
 <% end %>
                                                      submit




                                              ROR Lab.
Refactoring
Rendering Partial Collections
/app/views/posts/show.html.erb
                                               A post
 <h2>Comments</h2>
 <% @post.comments.each do |comment| %>
   <p>
     <b>Commenter:</b>
     <%= comment.commenter %>                  comments:
   </p>
                           _comment.html.erb
   <p>
     <b>Comment:</b>
     <%= comment.body %>
   </p>
 <% end %>
                                                           submit


        app/views/comments/_comment.html.erb

                                                   ROR Lab.
Refactoring
Rendering Partial Collections
/app/views/posts/show.html.erb
                                                A post
 <h2>Comments</h2>
 <% @post.comments.each do |comment| %>
   <p>
     <b>Commenter:</b>
     <%= comment.commenter %>                   comments:
   </p>
  
   <p>
     <b>Comment:</b>
     <%= comment.body %>
   </p>
 <% end %>
                                                            submit


         app/views/comments/_comment.html.erb

                                                    ROR Lab.
Refactoring
Rendering Partial Collections
/app/views/posts/show.html.erb
                                                A post
 <h2>Comments</h2>
 <% @post.comments.each do |comment| %>
   <p>
     <b>Commenter:</b>
     <%= comment.commenter %>                   comments:
   </p>
  
 <%= render :partial => “comments/comment” %>
   <p>
     <b>Comment:</b>
     <%= comment.body %>
   </p>
 <% end %>
                                                            submit


         app/views/comments/_comment.html.erb

                                                    ROR Lab.
Refactoring
Rendering Partial Collections
/app/views/posts/show.html.erb
                                                A post
 <h2>Comments</h2>
 <% @post.comments.each do |comment| %>
   <p>
     <b>Commenter:</b>
     <%= comment.commenter %>                   comments:
   </p>
  
 <%= render :partial => “comments/comment” %>
            @post.comments %>
   <p>
   
     <b>Comment:</b>
     <%= comment.body %>
   </p>
 <% end %>
                                                            submit


         app/views/comments/_comment.html.erb

                                                    ROR Lab.
Refactoring
Rendering Partial Collections
/app/views/posts/show.html.erb
                                                     A post
 <h2>Comments</h2>
 <% @post.comments.each do |comment| %>
   <p>
     <b>Commenter:</b>
     <%= comment.commenter %>                        comments:
   </p>
  
 <%= render :partial => “comments/comment” %>
            @post.comments %>
   <p>
   
     <b>Comment:</b>
     <%= comment.body %>
   </p>                            local variable,
 <% end %>                          comment
                                                                 submit


         app/views/comments/_comment.html.erb

                                                         ROR Lab.
Refactoring
Rendering a Partial Form
/app/views/posts/show.html.erb
                                                         A post
 <%= form_for([@post, @post.comments.build]) do |f| %>
   <div class="field">
     <%= f.label :commenter %><br />
     <%= f.text_field :commenter %>
   </div>
   <div class="field">                                    comments:
     <%= f.label :body %><br />
     <%= f.text_area :body %>
   </div>
   <div class="actions">
     <%= f.submit %>
   </div>
 <% end %>
                                                                     submit


           app/views/comments/_form.html.erb

                                                             ROR Lab.
Refactoring
Rendering a Partial Form
/app/views/posts/show.html.erb
                                                         A post
 <%= form_for([@post, @post.comments.build]) do |f| %>
   <div class="field">
     <%= f.label :commenter %><br />
     <%= f.text_field :commenter %>
   </div>
   <div class="field">                                    comments:
     <%= f.label :body %><br />
     <%= f.text_area :body %>
   </div>
   <div class="actions">
     <%= f.submit %>
   </div>
 <% end %>
                                                                     submit


           app/views/comments/_form.html.erb

                                                             ROR Lab.
app/views/posts/show.html.erb
                                                        A post
<p id="notice"><%= notice %></p>
 
<p>
  <b>Name:</b>
  <%= @post.name %>
</p>
 
<p>
  <b>Title:</b>                                         comments:
  <%= @post.title %>
</p>
 
<p>
  <b>Content:</b>
  <%= @post.content %>
</p>                                                                submit
 
<h2>Add a comment:</h2>
<%= form_for([@post, @post.comments.build]) do |f| %>
  <div class="field">
    <%= f.label :commenter %><br />
    <%= f.text_field :commenter %>
  </div>
  <div class="field">
    <%= f.label :body %><br />
    <%= f.text_area :body %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
 
<%= link_to 'Edit Post', edit_post_path(@post) %> |
<%= link_to 'Back to Posts', posts_path %> |
                                                          ROR Lab.
app/views/posts/show.html.erb
                                                        A post
<p id="notice"><%= notice %></p>
 
<p>
  <b>Name:</b>
  <%= @post.name %>
</p>
 
<p>
  <b>Title:</b>                                         comments:
  <%= @post.title %>
</p>
 
<p>
  <b>Content:</b>
  <%= @post.content %>
</p>                                                                submit
 
<h2>Add a comment:</h2>
<%= form_for([@post, @post.comments.build]) do |f| %>
  <div class="field">
    <%= f.label :commenter %><br />
    <%= f.text_field :commenter %>
  </div>
  <div class="field">
    <%= f.label :body %><br />
    <%= f.text_area :body %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
 
<%= link_to 'Edit Post', edit_post_path(@post) %> |
<%= link_to 'Back to Posts', posts_path %> |
                                                          ROR Lab.
app/views/posts/show.html.erb
                                                        A post
<p id="notice"><%= notice %></p>
 
<p>
  <b>Name:</b>
  <%= @post.name %>
</p>
 
<p>
  <b>Title:</b>                                         comments:
  <%= @post.title %>
</p>
 
<p>
  <b>Content:</b>
  <%= @post.content %>
</p>                                                                submit
 
<h2>Comments</h2>
<h2>Add a comment:</h2>
<% @post.comments.each do |comment| %>
<%= form_for([@post, @post.comments.build]) do |f| %>
<%= render “comments/comment” %>
  <div class="field">
<% end %>
    <%= f.label :commenter %><br />
    <%= f.text_field :commenter %>
or
  </div>
  <div class="field">
<h2>Comments</h2>
    <%= f.label :body %><br />
<%= render @post.comments %>
    <%= f.text_area :body %>
  </div>
  <div class="actions">
<h2>Add a comment:</h2>
    <%= f.submit %>
<%= render "comments/form" %>
  </div>
<% end %>
 
<%= link_to 'Edit Post', edit_post_path(@post) %> |
<%= link_to 'Back to Posts', posts_path %> |
                                                          ROR Lab.
Deleting
         Comments
app/views/comments/_comment.html.erb

<p>
  <b>Commenter:</b>
  <%= comment.commenter %>
</p>
 
<p>
  <b>Comment:</b>
  <%= comment.body %>
</p>
 
<p>
  <%= link_to 'Destroy Comment', [comment.post, comment],
               :confirm => 'Are you sure?',
               :method => :delete %>
</p>


DELETE /posts/:post_id/comments/:id
                                                            ROR Lab.
Deleting
         Comments
app/views/comments/_comment.html.erb

<p>
  <b>Commenter:</b>
  <%= comment.commenter %>
</p>
 
<p>
  <b>Comment:</b>
  <%= comment.body %>
</p>
 
<p>
  <%= link_to 'Destroy Comment', [comment.post, comment],
               :confirm => 'Are you sure?',
               :method => :delete %>
</p>


DELETE /posts/:post_id/comments/:id
                                                            ROR Lab.
Deleting
           Comments
DELETE /posts/:post_id/comments/:id


  <p>
    <b>Commenter:</b>
    <%= comment.commenter %>
  </p>
   
  <p>
    <b>Comment:</b>
    <%= comment.body %>
  </p>
   
  <p>
    <%= link_to 'Destroy Comment', [comment.post, comment],
                 :confirm => 'Are you sure?',
                 :method => :delete %>
  </p>




                                                              ROR Lab.
Deleting
           Comments
DELETE /posts/:post_id/comments/:id


  <p>
    <b>Commenter:</b>
    <%= comment.commenter %>
  </p>
   
  <p>
    <b>Comment:</b>
    <%= comment.body %>
  </p>
   
  <p>
    <%= link_to 'Destroy Comment', [comment.post, comment],
                 :confirm => 'Are you sure?',
                 :method => :delete %>
  </p>




                                                              ROR Lab.
Deleting
                        Comments
                         DELETE /posts/:post_id/comments/:id

 $ rake routes
   post_comments GET /posts/:post_id/comments(.:format)            comments#index
             POST /posts/:post_id/comments(.:format)        comments#create
 new_post_comment GET /posts/:post_id/comments/new(.:format)           comments#new
edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) comments#edit
    post_comment GET /posts/:post_id/comments/:id(.:format)         comments#show
             PUT /posts/:post_id/comments/:id(.:format)     comments#update
             DELETE /posts/:post_id/comments/:id(.:format)    comments#destroy
         posts GET /posts(.:format)                  posts#index
             POST /posts(.:format)                  posts#create
       new_post GET /posts/new(.:format)                  posts#new
      edit_post GET /posts/:id/edit(.:format)            posts#edit
          post GET /posts/:id(.:format)               posts#show
             PUT /posts/:id(.:format)               posts#update
             DELETE /posts/:id(.:format)              posts#destroy



                                                                                 ROR Lab.
Deleting
                        Comments
                         DELETE /posts/:post_id/comments/:id

 $ rake routes
   post_comments GET /posts/:post_id/comments(.:format)            comments#index
             POST /posts/:post_id/comments(.:format)        comments#create
 new_post_comment GET /posts/:post_id/comments/new(.:format)           comments#new
edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) comments#edit
    post_comment GET /posts/:post_id/comments/:id(.:format)         comments#show
             PUT /posts/:post_id/comments/:id(.:format)     comments#update
             DELETE /posts/:post_id/comments/:id(.:format)    comments#destroy
         posts GET /posts(.:format)                  posts#index
             POST /posts(.:format)                  posts#create
       new_post GET /posts/new(.:format)                  posts#new
      edit_post GET /posts/:id/edit(.:format)            posts#edit
          post GET /posts/:id(.:format)               posts#show
             PUT /posts/:id(.:format)               posts#update
             DELETE /posts/:id(.:format)              posts#destroy



                                                                                 ROR Lab.
Deleting
        Comments
class CommentsController < ApplicationController
 
  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.create(params[:comment])
    redirect_to post_path(@post)
  end
 
  def destroy
    @post = Post.find(params[:post_id])
    @comment = @post.comments.find(params[:id])
    @comment.destroy
    redirect_to post_path(@post)
  end
 
end




                                                         ROR Lab.
Deleting
        Comments
class CommentsController < ApplicationController
 
  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.create(params[:comment])
    redirect_to post_path(@post)
  end
 
  def destroy
    @post = Post.find(params[:post_id])
    @comment = @post.comments.find(params[:id])
    @comment.destroy
    redirect_to post_path(@post)
  end
 
end




                                                         ROR Lab.
Deleting
  Assoc. Objects
class Post < ActiveRecord::Base
  attr_accessible :content, :name, :title
 
  validates :name,  :presence => true
  validates :title, :presence => true,
                    :length => { :minimum => 5 }
  has_many :comments, :dependent => :destroy


                                             app/models/post.rb




                                                         ROR Lab.
Deleting
    Assoc. Objects
 class Post < ActiveRecord::Base
   attr_accessible :content, :name, :title
  
   validates :name,  :presence => true
   validates :title, :presence => true,
                     :length => { :minimum => 5 }
   has_many :comments, :dependent => :destroy


                                              app/models/post.rb

Other options:     :dependent => :destroy
                   :dependent => :delete
                   :dependent => :nullify

                                                          ROR Lab.
Security
A very simple HTTP authentication system

 class PostsController < ApplicationController
  
   http_basic_authenticate_with
      :name => "dhh",
      :password => "secret",
      :except => [:index, :show]
  
   # GET /posts
   # GET /posts.json
   def index
     @posts = Post.all
     respond_to do |format|




                                                 ROR Lab.
Security
A very simple HTTP authentication system

 class PostsController < ApplicationController
  
   http_basic_authenticate_with
      :name => "dhh",
      :password => "secret",
      :except => [:index, :show]
  
   # GET /posts
   # GET /posts.json
   def index
     @posts = Post.all
     respond_to do |format|




                                                 ROR Lab.
Security
A very simple HTTP authentication system

 class CommentsController < ApplicationController
  
   http_basic_authenticate_with
     :name => "dhh",
     :password => "secret",
     :only => :destroy
  
   def create
     @post = Post.find(params[:post_id])




                                                    ROR Lab.
Security
A very simple HTTP authentication system

 class CommentsController < ApplicationController
  
   http_basic_authenticate_with
     :name => "dhh",
     :password => "secret",
     :only => :destroy
  
   def create
     @post = Post.find(params[:post_id])




                                                    ROR Lab.
Security
A very simple HTTP authentication system




                                    ROR Lab.
Live Demo
Creating a project ~ First model, Post




                                    ROR Lab.
감사합니다.

More Related Content

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

Getting started with Rails (2), Season 2
Getting started with Rails (2), Season 2Getting started with Rails (2), Season 2
Getting started with Rails (2), Season 2RORLAB
 
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
 
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
 
Ruby on rails3 - introduction to rails
Ruby on rails3 - introduction to railsRuby on rails3 - introduction to rails
Ruby on rails3 - introduction to railsEmad Elsaid
 
Getting Started with Rails (4)
Getting Started with Rails (4) Getting Started with Rails (4)
Getting Started with Rails (4) RORLAB
 
RoR 101: Session 2
RoR 101: Session 2RoR 101: Session 2
RoR 101: Session 2Rory Gianni
 
Routing 1, Season 1
Routing 1, Season 1Routing 1, Season 1
Routing 1, Season 1RORLAB
 
Trailblazer Introduction by Nick Sutterer
Trailblazer Introduction by Nick SuttererTrailblazer Introduction by Nick Sutterer
Trailblazer Introduction by Nick SuttererPivorak MeetUp
 
More to RoC weibo
More to RoC weiboMore to RoC weibo
More to RoC weiboshaokun
 
OSDC 2009 Rails Turtorial
OSDC 2009 Rails TurtorialOSDC 2009 Rails Turtorial
OSDC 2009 Rails TurtorialYi-Ting Cheng
 
RubyConf Bangladesh 2017 - Elixir for Rubyists
RubyConf Bangladesh 2017 - Elixir for RubyistsRubyConf Bangladesh 2017 - Elixir for Rubyists
RubyConf Bangladesh 2017 - Elixir for RubyistsRuby Bangladesh
 
Pourquoi ruby et rails déchirent
Pourquoi ruby et rails déchirentPourquoi ruby et rails déchirent
Pourquoi ruby et rails déchirentNicolas Ledez
 
Drupal 7 module development
Drupal 7 module developmentDrupal 7 module development
Drupal 7 module developmentAdam Kalsey
 
Rails vs Web2py
Rails vs Web2pyRails vs Web2py
Rails vs Web2pyjonromero
 
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
 
ActiveResource & REST
ActiveResource & RESTActiveResource & REST
ActiveResource & RESTRobbert
 

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

Getting started with Rails (2), Season 2
Getting started with Rails (2), Season 2Getting started with Rails (2), Season 2
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
 
Introduction à Ruby
Introduction à RubyIntroduction à Ruby
Introduction à Ruby
 
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
 
Cocoa on-rails-3rd
Cocoa on-rails-3rdCocoa on-rails-3rd
Cocoa on-rails-3rd
 
Ruby on rails3 - introduction to rails
Ruby on rails3 - introduction to railsRuby on rails3 - introduction to rails
Ruby on rails3 - introduction to rails
 
Getting Started with Rails (4)
Getting Started with Rails (4) Getting Started with Rails (4)
Getting Started with Rails (4)
 
RoR 101: Session 2
RoR 101: Session 2RoR 101: Session 2
RoR 101: Session 2
 
Routing 1, Season 1
Routing 1, Season 1Routing 1, Season 1
Routing 1, Season 1
 
Trailblazer Introduction by Nick Sutterer
Trailblazer Introduction by Nick SuttererTrailblazer Introduction by Nick Sutterer
Trailblazer Introduction by Nick Sutterer
 
More to RoC weibo
More to RoC weiboMore to RoC weibo
More to RoC weibo
 
OSDC 2009 Rails Turtorial
OSDC 2009 Rails TurtorialOSDC 2009 Rails Turtorial
OSDC 2009 Rails Turtorial
 
RubyConf Bangladesh 2017 - Elixir for Rubyists
RubyConf Bangladesh 2017 - Elixir for RubyistsRubyConf Bangladesh 2017 - Elixir for Rubyists
RubyConf Bangladesh 2017 - Elixir for Rubyists
 
Rails introduction
Rails introductionRails introduction
Rails introduction
 
Pourquoi ruby et rails déchirent
Pourquoi ruby et rails déchirentPourquoi ruby et rails déchirent
Pourquoi ruby et rails déchirent
 
Drupal 7 module development
Drupal 7 module developmentDrupal 7 module development
Drupal 7 module development
 
Rails vs Web2py
Rails vs Web2pyRails vs Web2py
Rails vs Web2py
 
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
 
The Rails Way
The Rails WayThe Rails Way
The Rails Way
 
ActiveResource & REST
ActiveResource & RESTActiveResource & REST
ActiveResource & REST
 

More from 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
 
Asset Pipeline in Ruby on Rails
Asset Pipeline in Ruby on RailsAsset Pipeline in Ruby on Rails
Asset Pipeline in Ruby on RailsRORLAB
 
레일스가이드 한글번역 공개프로젝트 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
 
Action Controller Overview, Season 2
Action Controller Overview, Season 2Action Controller Overview, Season 2
Action Controller Overview, Season 2RORLAB
 
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
 
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
 
Layouts and Rendering in Rails, Season 2
Layouts and Rendering in Rails, Season 2Layouts and Rendering in Rails, Season 2
Layouts and Rendering in Rails, 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 (1), Season 2
Getting started with Rails (1), Season 2Getting started with Rails (1), Season 2
Getting started with Rails (1), Season 2RORLAB
 
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
 
Active Record Query Interface (2), Season 1
Active Record Query Interface (2), Season 1Active Record Query Interface (2), Season 1
Active Record Query Interface (2), Season 1RORLAB
 

More from RORLAB (20)

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
 
Asset Pipeline in Ruby on Rails
Asset Pipeline in Ruby on RailsAsset Pipeline in Ruby on Rails
Asset Pipeline in Ruby on Rails
 
레일스가이드 한글번역 공개프로젝트 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)
 
Action Controller Overview, Season 2
Action Controller Overview, Season 2Action Controller Overview, Season 2
Action Controller Overview, Season 2
 
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
 
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
 
Layouts and Rendering in Rails, Season 2
Layouts and Rendering in Rails, Season 2Layouts and Rendering in Rails, Season 2
Layouts and Rendering in Rails, 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 (1), Season 2
Getting started with Rails (1), Season 2Getting started with Rails (1), Season 2
Getting started with Rails (1), Season 2
 
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
 
Active Record Query Interface (2), Season 1
Active Record Query Interface (2), Season 1Active Record Query Interface (2), Season 1
Active Record Query Interface (2), Season 1
 

Recently uploaded

How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 

Recently uploaded (20)

How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 

Getting started with Rails (3), Season 2

  • 1. ROR Lab. Season 2 - The 3rd Round - Getting Started with Rails (3) July 21, 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. Adding a Second Model Class Database Comment ORM comments singular plural ROR Lab.
  • 4. Adding a Second Model Model Class Database Table Comment Active Record comments object record attributes fields ROR Lab.
  • 5. Generating a Model $ rails generate model Comment commenter:string body:text post:references generate scaffold generate model generate controller generate migration ROR Lab.
  • 6. Generating a Model $ rails generate model Comment commenter:string body:text Migration file Model Class : db/migrate/xxxx_create_comment.rb : app/models/comment.rb Comment Comments belongs_to :post post_id :integer post:references ROR Lab.
  • 7. Generating a Model class Comment < ActiveRecord::Base   belongs_to :post end Model class file @comment.post class CreateComments < ActiveRecord::Migration   def change     create_table :comments do |t|       t.string :commenter       t.text :body       t.references :post         t.timestamps Migration file     end       add_index :comments, :post_id   end end $ rake db:migrate ROR Lab.
  • 8. Associating Models Parent Model Relation Child Model has_many Post Comment belongs_to app/models/post.rb app/models/comment.rb ROR Lab.
  • 9. Associating Models class Post < ActiveRecord::Base   attr_accessible :content, :name, :title     validates :name,  :presence => true   validates :title, :presence => true,                     :length => { :minimum => 5 }     has_many :comments end app/models/post.rb Automatic behavior : @post.comments ROR Lab.
  • 10. Adding a Route config/routes.rb resources :posts do   resources :comments end ROR Lab.
  • 11. Generating a Controller $ rails generate controller Comments ROR Lab.
  • 12. app/views/posts/show.html.erb A post <p id="notice"><%= notice %></p>   <p>   <b>Name:</b>   <%= @post.name %> </p>   <p>   <b>Title:</b> comments:   <%= @post.title %> </p>   <p>   <b>Content:</b>   <%= @post.content %> </p> submit   <h2>Add a comment:</h2> <%= form_for([@post, @post.comments.build]) do |f| %>   <div class="field">     <%= f.label :commenter %><br />     <%= f.text_field :commenter %>   </div>   <div class="field">     <%= f.label :body %><br />     <%= f.text_area :body %>   </div>   <div class="actions">     <%= f.submit %>   </div> <% end %>   <%= link_to 'Edit Post', edit_post_path(@post) %> | <%= link_to 'Back to Posts', posts_path %> | ROR Lab.
  • 13. app/views/posts/show.html.erb A post <p id="notice"><%= notice %></p>   <p>   <b>Name:</b>   <%= @post.name %> </p>   <p>   <b>Title:</b> comments:   <%= @post.title %> </p>   <p>   <b>Content:</b>   <%= @post.content %> </p> submit   <h2>Add a comment:</h2> <%= form_for([@post, @post.comments.build]) do |f| %>   <div class="field">     <%= f.label :commenter %><br />     <%= f.text_field :commenter %>   </div>   <div class="field">     <%= f.label :body %><br />     <%= f.text_area :body %>   </div>   <div class="actions">     <%= f.submit %>   </div> <% end %>   <%= link_to 'Edit Post', edit_post_path(@post) %> | <%= link_to 'Back to Posts', posts_path %> | ROR Lab.
  • 14. app/views/posts/show.html.erb A post <p id="notice"><%= notice %></p>   <p>   <b>Name:</b>   <%= @post.name %> </p>   <p>   <b>Title:</b> comments:   <%= @post.title %> </p>   <p>   <b>Content:</b>   <%= @post.content %> </p> submit   <h2>Add a comment:</h2> <%= form_for([@post, @post.comments.build]) do |f| %>   <div class="field">     <%= f.label :commenter %><br />     <%= f.text_field :commenter %>   </div>   <div class="field">     <%= f.label :body %><br />     <%= f.text_area :body %>   </div>   <div class="actions">     <%= f.submit %>   </div> <% end %>   <%= link_to 'Edit Post', edit_post_path(@post) %> | <%= link_to 'Back to Posts', posts_path %> | ROR Lab.
  • 15. Creating a Comment $ rails generate controller Comments create destroy /app/controllers/comments_controller.rb class CommentsController < ApplicationController   def create     @post = Post.find(params[:post_id])     @comment = @post.comments.create(params[:comment])     redirect_to post_path(@post)   end end ROR Lab.
  • 16. Creating a Comment /app/views/posts/show.html.erb A post <h2>Comments</h2> <% @post.comments.each do |comment| %>   <p>     <b>Commenter:</b>     <%= comment.commenter %>   </p>     <p> comments:     <b>Comment:</b>     <%= comment.body %>   </p> <% end %> submit ROR Lab.
  • 17. Creating a Comment /app/views/posts/show.html.erb A post <h2>Comments</h2> <% @post.comments.each do |comment| %>   <p>     <b>Commenter:</b>     <%= comment.commenter %>   </p>     <p> comments:     <b>Comment:</b>     <%= comment.body %>   </p> <% end %> submit ROR Lab.
  • 18. Refactoring • Getting long and awkward • Using “partials” to clean up ROR Lab.
  • 19. Refactoring Rendering Partial Collections /app/views/posts/show.html.erb A post <h2>Comments</h2> <% @post.comments.each do |comment| %>   <p>     <b>Commenter:</b>     <%= comment.commenter %> comments:   </p>     <p>     <b>Comment:</b>     <%= comment.body %>   </p> <% end %> submit ROR Lab.
  • 20. Refactoring Rendering Partial Collections /app/views/posts/show.html.erb A post <h2>Comments</h2> <% @post.comments.each do |comment| %>   <p>     <b>Commenter:</b>     <%= comment.commenter %> comments:   </p>     <p>     <b>Comment:</b>     <%= comment.body %>   </p> <% end %> submit ROR Lab.
  • 21. Refactoring Rendering Partial Collections /app/views/posts/show.html.erb A post <h2>Comments</h2> <% @post.comments.each do |comment| %>   <p>     <b>Commenter:</b>     <%= comment.commenter %> comments:   </p>     <p>     <b>Comment:</b>     <%= comment.body %>   </p> <% end %> submit ROR Lab.
  • 22. Refactoring Rendering Partial Collections /app/views/posts/show.html.erb A post <h2>Comments</h2> <% @post.comments.each do |comment| %>   <p>     <b>Commenter:</b>     <%= comment.commenter %> comments:   </p>   _comment.html.erb   <p>     <b>Comment:</b>     <%= comment.body %>   </p> <% end %> submit app/views/comments/_comment.html.erb ROR Lab.
  • 23. Refactoring Rendering Partial Collections /app/views/posts/show.html.erb A post <h2>Comments</h2> <% @post.comments.each do |comment| %>   <p>     <b>Commenter:</b>     <%= comment.commenter %> comments:   </p>     <p>     <b>Comment:</b>     <%= comment.body %>   </p> <% end %> submit app/views/comments/_comment.html.erb ROR Lab.
  • 24. Refactoring Rendering Partial Collections /app/views/posts/show.html.erb A post <h2>Comments</h2> <% @post.comments.each do |comment| %>   <p>     <b>Commenter:</b>     <%= comment.commenter %> comments:   </p>   <%= render :partial => “comments/comment” %>   <p>     <b>Comment:</b>     <%= comment.body %>   </p> <% end %> submit app/views/comments/_comment.html.erb ROR Lab.
  • 25. Refactoring Rendering Partial Collections /app/views/posts/show.html.erb A post <h2>Comments</h2> <% @post.comments.each do |comment| %>   <p>     <b>Commenter:</b>     <%= comment.commenter %> comments:   </p>   <%= render :partial => “comments/comment” %> @post.comments %>   <p>        <b>Comment:</b>     <%= comment.body %>   </p> <% end %> submit app/views/comments/_comment.html.erb ROR Lab.
  • 26. Refactoring Rendering Partial Collections /app/views/posts/show.html.erb A post <h2>Comments</h2> <% @post.comments.each do |comment| %>   <p>     <b>Commenter:</b>     <%= comment.commenter %> comments:   </p>   <%= render :partial => “comments/comment” %> @post.comments %>   <p>        <b>Comment:</b>     <%= comment.body %>   </p> local variable, <% end %> comment submit app/views/comments/_comment.html.erb ROR Lab.
  • 27. Refactoring Rendering a Partial Form /app/views/posts/show.html.erb A post <%= form_for([@post, @post.comments.build]) do |f| %>   <div class="field">     <%= f.label :commenter %><br />     <%= f.text_field :commenter %>   </div>   <div class="field"> comments:     <%= f.label :body %><br />     <%= f.text_area :body %>   </div>   <div class="actions">     <%= f.submit %>   </div> <% end %> submit app/views/comments/_form.html.erb ROR Lab.
  • 28. Refactoring Rendering a Partial Form /app/views/posts/show.html.erb A post <%= form_for([@post, @post.comments.build]) do |f| %>   <div class="field">     <%= f.label :commenter %><br />     <%= f.text_field :commenter %>   </div>   <div class="field"> comments:     <%= f.label :body %><br />     <%= f.text_area :body %>   </div>   <div class="actions">     <%= f.submit %>   </div> <% end %> submit app/views/comments/_form.html.erb ROR Lab.
  • 29. app/views/posts/show.html.erb A post <p id="notice"><%= notice %></p>   <p>   <b>Name:</b>   <%= @post.name %> </p>   <p>   <b>Title:</b> comments:   <%= @post.title %> </p>   <p>   <b>Content:</b>   <%= @post.content %> </p> submit   <h2>Add a comment:</h2> <%= form_for([@post, @post.comments.build]) do |f| %>   <div class="field">     <%= f.label :commenter %><br />     <%= f.text_field :commenter %>   </div>   <div class="field">     <%= f.label :body %><br />     <%= f.text_area :body %>   </div>   <div class="actions">     <%= f.submit %>   </div> <% end %>   <%= link_to 'Edit Post', edit_post_path(@post) %> | <%= link_to 'Back to Posts', posts_path %> | ROR Lab.
  • 30. app/views/posts/show.html.erb A post <p id="notice"><%= notice %></p>   <p>   <b>Name:</b>   <%= @post.name %> </p>   <p>   <b>Title:</b> comments:   <%= @post.title %> </p>   <p>   <b>Content:</b>   <%= @post.content %> </p> submit   <h2>Add a comment:</h2> <%= form_for([@post, @post.comments.build]) do |f| %>   <div class="field">     <%= f.label :commenter %><br />     <%= f.text_field :commenter %>   </div>   <div class="field">     <%= f.label :body %><br />     <%= f.text_area :body %>   </div>   <div class="actions">     <%= f.submit %>   </div> <% end %>   <%= link_to 'Edit Post', edit_post_path(@post) %> | <%= link_to 'Back to Posts', posts_path %> | ROR Lab.
  • 31. app/views/posts/show.html.erb A post <p id="notice"><%= notice %></p>   <p>   <b>Name:</b>   <%= @post.name %> </p>   <p>   <b>Title:</b> comments:   <%= @post.title %> </p>   <p>   <b>Content:</b>   <%= @post.content %> </p> submit   <h2>Comments</h2> <h2>Add a comment:</h2> <% @post.comments.each do |comment| %> <%= form_for([@post, @post.comments.build]) do |f| %> <%= render “comments/comment” %>   <div class="field"> <% end %>     <%= f.label :commenter %><br />     <%= f.text_field :commenter %> or   </div>   <div class="field"> <h2>Comments</h2>     <%= f.label :body %><br /> <%= render @post.comments %>     <%= f.text_area :body %>   </div>   <div class="actions"> <h2>Add a comment:</h2>     <%= f.submit %> <%= render "comments/form" %>   </div> <% end %>   <%= link_to 'Edit Post', edit_post_path(@post) %> | <%= link_to 'Back to Posts', posts_path %> | ROR Lab.
  • 32. Deleting Comments app/views/comments/_comment.html.erb <p>   <b>Commenter:</b>   <%= comment.commenter %> </p>   <p>   <b>Comment:</b>   <%= comment.body %> </p>   <p>   <%= link_to 'Destroy Comment', [comment.post, comment],                :confirm => 'Are you sure?',                :method => :delete %> </p> DELETE /posts/:post_id/comments/:id ROR Lab.
  • 33. Deleting Comments app/views/comments/_comment.html.erb <p>   <b>Commenter:</b>   <%= comment.commenter %> </p>   <p>   <b>Comment:</b>   <%= comment.body %> </p>   <p>   <%= link_to 'Destroy Comment', [comment.post, comment],                :confirm => 'Are you sure?',                :method => :delete %> </p> DELETE /posts/:post_id/comments/:id ROR Lab.
  • 34. Deleting Comments DELETE /posts/:post_id/comments/:id <p>   <b>Commenter:</b>   <%= comment.commenter %> </p>   <p>   <b>Comment:</b>   <%= comment.body %> </p>   <p>   <%= link_to 'Destroy Comment', [comment.post, comment],                :confirm => 'Are you sure?',                :method => :delete %> </p> ROR Lab.
  • 35. Deleting Comments DELETE /posts/:post_id/comments/:id <p>   <b>Commenter:</b>   <%= comment.commenter %> </p>   <p>   <b>Comment:</b>   <%= comment.body %> </p>   <p>   <%= link_to 'Destroy Comment', [comment.post, comment],                :confirm => 'Are you sure?',                :method => :delete %> </p> ROR Lab.
  • 36. Deleting Comments DELETE /posts/:post_id/comments/:id $ rake routes post_comments GET /posts/:post_id/comments(.:format) comments#index POST /posts/:post_id/comments(.:format) comments#create new_post_comment GET /posts/:post_id/comments/new(.:format) comments#new edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) comments#edit post_comment GET /posts/:post_id/comments/:id(.:format) comments#show PUT /posts/:post_id/comments/:id(.:format) comments#update DELETE /posts/:post_id/comments/:id(.:format) comments#destroy posts GET /posts(.:format) posts#index POST /posts(.:format) posts#create new_post GET /posts/new(.:format) posts#new edit_post GET /posts/:id/edit(.:format) posts#edit post GET /posts/:id(.:format) posts#show PUT /posts/:id(.:format) posts#update DELETE /posts/:id(.:format) posts#destroy ROR Lab.
  • 37. Deleting Comments DELETE /posts/:post_id/comments/:id $ rake routes post_comments GET /posts/:post_id/comments(.:format) comments#index POST /posts/:post_id/comments(.:format) comments#create new_post_comment GET /posts/:post_id/comments/new(.:format) comments#new edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) comments#edit post_comment GET /posts/:post_id/comments/:id(.:format) comments#show PUT /posts/:post_id/comments/:id(.:format) comments#update DELETE /posts/:post_id/comments/:id(.:format) comments#destroy posts GET /posts(.:format) posts#index POST /posts(.:format) posts#create new_post GET /posts/new(.:format) posts#new edit_post GET /posts/:id/edit(.:format) posts#edit post GET /posts/:id(.:format) posts#show PUT /posts/:id(.:format) posts#update DELETE /posts/:id(.:format) posts#destroy ROR Lab.
  • 38. Deleting Comments class CommentsController < ApplicationController     def create     @post = Post.find(params[:post_id])     @comment = @post.comments.create(params[:comment])     redirect_to post_path(@post)   end     def destroy     @post = Post.find(params[:post_id])     @comment = @post.comments.find(params[:id])     @comment.destroy     redirect_to post_path(@post)   end   end ROR Lab.
  • 39. Deleting Comments class CommentsController < ApplicationController     def create     @post = Post.find(params[:post_id])     @comment = @post.comments.create(params[:comment])     redirect_to post_path(@post)   end     def destroy     @post = Post.find(params[:post_id])     @comment = @post.comments.find(params[:id])     @comment.destroy     redirect_to post_path(@post)   end   end ROR Lab.
  • 40. Deleting Assoc. Objects class Post < ActiveRecord::Base   attr_accessible :content, :name, :title     validates :name,  :presence => true   validates :title, :presence => true,                     :length => { :minimum => 5 }   has_many :comments, :dependent => :destroy app/models/post.rb ROR Lab.
  • 41. Deleting Assoc. Objects class Post < ActiveRecord::Base   attr_accessible :content, :name, :title     validates :name,  :presence => true   validates :title, :presence => true,                     :length => { :minimum => 5 }   has_many :comments, :dependent => :destroy app/models/post.rb Other options: :dependent => :destroy :dependent => :delete :dependent => :nullify ROR Lab.
  • 42. Security A very simple HTTP authentication system class PostsController < ApplicationController     http_basic_authenticate_with :name => "dhh", :password => "secret", :except => [:index, :show]     # GET /posts   # GET /posts.json   def index     @posts = Post.all     respond_to do |format| ROR Lab.
  • 43. Security A very simple HTTP authentication system class PostsController < ApplicationController     http_basic_authenticate_with :name => "dhh", :password => "secret", :except => [:index, :show]     # GET /posts   # GET /posts.json   def index     @posts = Post.all     respond_to do |format| ROR Lab.
  • 44. Security A very simple HTTP authentication system class CommentsController < ApplicationController     http_basic_authenticate_with :name => "dhh", :password => "secret", :only => :destroy     def create     @post = Post.find(params[:post_id]) ROR Lab.
  • 45. Security A very simple HTTP authentication system class CommentsController < ApplicationController     http_basic_authenticate_with :name => "dhh", :password => "secret", :only => :destroy     def create     @post = Post.find(params[:post_id]) ROR Lab.
  • 46. Security A very simple HTTP authentication system ROR Lab.
  • 47. Live Demo Creating a project ~ First model, Post ROR Lab.
  • 49.   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
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n