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

Weitere ähnliche Inhalte

Ähnlich wie Getting started with Rails (3), Season 2

OSDC 2009 Rails Turtorial
OSDC 2009 Rails TurtorialOSDC 2009 Rails Turtorial
OSDC 2009 Rails Turtorial
Yi-Ting Cheng
 
Drupal 7 module development
Drupal 7 module developmentDrupal 7 module development
Drupal 7 module development
Adam Kalsey
 
Be happy with Ruby on Rails - CEUNSP Itu
Be happy with Ruby on Rails - CEUNSP ItuBe happy with Ruby on Rails - CEUNSP Itu
Be happy with Ruby on Rails - CEUNSP Itu
Lucas Renan
 

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

Mehr von 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 2
RORLAB
 
Action View Form Helpers - 2, Season 2
Action View Form Helpers - 2, Season 2Action View Form Helpers - 2, Season 2
Action View Form Helpers - 2, Season 2
RORLAB
 
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
RORLAB
 
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
RORLAB
 
ActiveRecord Query Interface (2), Season 2
ActiveRecord Query Interface (2), Season 2ActiveRecord Query Interface (2), Season 2
ActiveRecord Query Interface (2), Season 2
RORLAB
 
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
RORLAB
 

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

Kürzlich hochgeladen

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Kürzlich hochgeladen (20)

Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 

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.

Hinweis der Redaktion

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