Getting started with Rails (3), Season 2

1,698 views

Published on

This is a Keynote slide about the "Getting started with Rails

Published in: Education, Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,698
On SlideShare
0
From Embeds
0
Number of Embeds
998
Actions
Shares
0
Downloads
12
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Getting started with Rails (3), Season 2

    1. 1. ROR Lab. Season 2 - The 3rd Round -Getting Started with Rails (3) July 21, 2012 Hyoseong Choi ROR Lab.
    2. 2. A Blog Project Post ny on a mseparate form nested form e to form_for to ne m an o y Comment Tag form_for fields_for ROR Lab.
    3. 3. Addinga Second Model Class DatabaseComment ORM comments singular plural ROR Lab.
    4. 4. Addinga Second ModelModel Class Database TableComment Active Record comments object recordattributes fields ROR Lab.
    5. 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. 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 Commentsbelongs_to :post post_id :integer post:references ROR Lab.
    7. 7. Generating a Modelclass Comment < ActiveRecord::Base  belongs_to :postend Model class file @comment.postclass 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  endend $ rake db:migrate ROR Lab.
    8. 8. Associating Models Parent Model Relation Child Model has_many Post Comment belongs_toapp/models/post.rb app/models/comment.rb ROR Lab.
    9. 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.rbAutomatic behavior : @post.comments ROR Lab.
    10. 10. Adding a Routeconfig/routes.rbresources :posts do  resources :commentsend ROR Lab.
    11. 11. Generating a Controller$ rails generate controller Comments ROR Lab.
    12. 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. 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. 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. 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. 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. 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. 18. Refactoring• Getting long and awkward• Using “partials” to clean up ROR Lab.
    19. 19. RefactoringRendering 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. 20. RefactoringRendering 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. 21. RefactoringRendering 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. 22. RefactoringRendering 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. 23. RefactoringRendering 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. 24. RefactoringRendering 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. 25. RefactoringRendering 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. 26. RefactoringRendering 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. 27. RefactoringRendering 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. 28. RefactoringRendering 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. 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. 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. 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. 32. Deleting Commentsapp/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. 33. Deleting Commentsapp/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. 34. Deleting CommentsDELETE /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. 35. Deleting CommentsDELETE /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. 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#newedit_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. 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#newedit_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. 38. Deleting Commentsclass 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. 39. Deleting Commentsclass 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. 40. Deleting Assoc. Objectsclass 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. 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.rbOther options: :dependent => :destroy :dependent => :delete :dependent => :nullify ROR Lab.
    42. 42. SecurityA 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. 43. SecurityA 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. 44. SecurityA 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. 45. SecurityA 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. 46. SecurityA very simple HTTP authentication system ROR Lab.
    47. 47. Live DemoCreating a project ~ First model, Post ROR Lab.
    48. 48. 감사합니다.
    49. 49.   ROR Lab.

    ×