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

HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
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
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
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
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
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
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 

Recently uploaded (20)

HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
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)
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
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
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
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
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.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