SlideShare uma empresa Scribd logo
1 de 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.
감사합니다.

Mais conteúdo relacionado

Semelhante a 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
 

Semelhante a 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
 

Mais de 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
 

Mais de 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
 

Último

Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 

Último (20)

Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 

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.

Notas do Editor

  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