Getting started with Rails (3), Season 2

48
Getting Started with Rails (3) - The 3rd Round - July 21, 2012 Hyoseong Choi ROR Lab. ROR Lab. Season 2

Transcript of Getting started with Rails (3), Season 2

Page 1: Getting started with Rails (3), Season 2

Getting Started with Rails (3)

- The 3rd Round -

July 21, 2012

Hyoseong ChoiROR Lab.

ROR Lab. Season 2

Page 2: Getting started with Rails (3), Season 2

ROR Lab.

A Blog Project

Post

Comment Tag

one to manyone t

o man

y

nested formseparate form

fields_for

form_for

form_for

Page 3: Getting started with Rails (3), Season 2

ROR Lab.

Addinga Second Model

Comment

Class

singularcomments

plural

Database

ORM

Page 4: Getting started with Rails (3), Season 2

ROR Lab.

Addinga Second Model

Comment

Model Class

comments

Database Table

ActiveRecord

object recordattributes fields

Page 5: Getting started with Rails (3), Season 2

ROR Lab.

Generating a Model

$ rails generate model Comment commenter:string body:text post:references

generate scaffoldgenerate modelgenerate controllergenerate migration

Page 6: Getting started with Rails (3), Season 2

ROR Lab.

Generating a Model

$ rails generate model Comment commenter:string body:text

Comment

post:references

belongs_to :post post_id :integer

Comments

Model Class : app/models/comment.rb

Migration file : db/migrate/xxxx_create_comment.rb

Page 7: Getting started with Rails (3), Season 2

ROR Lab.

Generating a Model

class Comment < ActiveRecord::Base  belongs_to :postend

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

Model class file

Migration file

$ rake db:migrate

@comment.post

Page 8: Getting started with Rails (3), Season 2

ROR Lab.

Associating Models

Post Comment

Parent Model Child Model

has_many

belongs_to

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

Relation

Page 9: Getting started with Rails (3), Season 2

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 :commentsend

app/models/post.rb

Automatic behavior :

@post.comments

Page 10: Getting started with Rails (3), Season 2

ROR Lab.

Adding a Route

resources :posts do  resources :commentsend

config/routes.rb

Page 11: Getting started with Rails (3), Season 2

ROR Lab.

Generating a Controller

$ rails generate controller Comments

Page 12: Getting started with Rails (3), Season 2

ROR Lab.

<p id="notice"><%= notice %></p> <p>  <b>Name:</b>  <%= @post.name %></p> <p>  <b>Title:</b>  <%= @post.title %></p> <p>  <b>Content:</b>  <%= @post.content %></p> <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 %> |

A post

comments:

submit

app/views/posts/show.html.erb

Page 13: Getting started with Rails (3), Season 2

ROR Lab.

<p id="notice"><%= notice %></p> <p>  <b>Name:</b>  <%= @post.name %></p> <p>  <b>Title:</b>  <%= @post.title %></p> <p>  <b>Content:</b>  <%= @post.content %></p> <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 %> |

A post

comments:

submit

app/views/posts/show.html.erb

Page 14: Getting started with Rails (3), Season 2

ROR Lab.

<p id="notice"><%= notice %></p> <p>  <b>Name:</b>  <%= @post.name %></p> <p>  <b>Title:</b>  <%= @post.title %></p> <p>  <b>Content:</b>  <%= @post.content %></p> <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 %> |

A post

comments:

submit

app/views/posts/show.html.erb

Page 15: Getting started with Rails (3), Season 2

ROR Lab.

Creating a Comment

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

$ rails generate controller Comments create destroy

/app/controllers/comments_controller.rb

Page 16: Getting started with Rails (3), Season 2

ROR Lab.

Creating a Comment

<h2>Comments</h2><% @post.comments.each do |comment| %>  <p>    <b>Commenter:</b>    <%= comment.commenter %>  </p>   <p>    <b>Comment:</b>    <%= comment.body %>  </p><% end %>

/app/views/posts/show.html.erbA post

comments:

submit

Page 17: Getting started with Rails (3), Season 2

ROR Lab.

Creating a Comment

<h2>Comments</h2><% @post.comments.each do |comment| %>  <p>    <b>Commenter:</b>    <%= comment.commenter %>  </p>   <p>    <b>Comment:</b>    <%= comment.body %>  </p><% end %>

/app/views/posts/show.html.erbA post

comments:

submit

Page 18: Getting started with Rails (3), Season 2

ROR Lab.

Refactoring

• Getting long and awkward

• Using “partials” to clean up

Page 19: Getting started with Rails (3), Season 2

ROR Lab.

Refactoring

<h2>Comments</h2><% @post.comments.each do |comment| %>  <p>    <b>Commenter:</b>    <%= comment.commenter %>  </p>   <p>    <b>Comment:</b>    <%= comment.body %>  </p><% end %>

/app/views/posts/show.html.erbA post

comments:

submit

Rendering Partial Collections

Page 20: Getting started with Rails (3), Season 2

ROR Lab.

Refactoring

<h2>Comments</h2><% @post.comments.each do |comment| %>  <p>    <b>Commenter:</b>    <%= comment.commenter %>  </p>   <p>    <b>Comment:</b>    <%= comment.body %>  </p><% end %>

/app/views/posts/show.html.erbA post

comments:

submit

Rendering Partial Collections

Page 21: Getting started with Rails (3), Season 2

ROR Lab.

Refactoring

<h2>Comments</h2><% @post.comments.each do |comment| %>  <p>    <b>Commenter:</b>    <%= comment.commenter %>  </p>   <p>    <b>Comment:</b>    <%= comment.body %>  </p><% end %>

/app/views/posts/show.html.erbA post

comments:

submit

Rendering Partial Collections

Page 22: Getting started with Rails (3), Season 2

ROR Lab.

Refactoring

<h2>Comments</h2><% @post.comments.each do |comment| %>  <p>    <b>Commenter:</b>    <%= comment.commenter %>  </p>   <p>    <b>Comment:</b>    <%= comment.body %>  </p><% end %>

/app/views/posts/show.html.erbA post

comments:

submit

_comment.html.erb

app/views/comments/_comment.html.erb

Rendering Partial Collections

Page 23: Getting started with Rails (3), Season 2

ROR Lab.

Refactoring

<h2>Comments</h2><% @post.comments.each do |comment| %>  <p>    <b>Commenter:</b>    <%= comment.commenter %>  </p>   <p>    <b>Comment:</b>    <%= comment.body %>  </p><% end %>

/app/views/posts/show.html.erbA post

comments:

submit

app/views/comments/_comment.html.erb

Rendering Partial Collections

Page 24: Getting started with Rails (3), Season 2

ROR Lab.

Refactoring

<h2>Comments</h2><% @post.comments.each do |comment| %>  <p>    <b>Commenter:</b>    <%= comment.commenter %>  </p>   <p>    <b>Comment:</b>    <%= comment.body %>  </p><% end %>

/app/views/posts/show.html.erbA post

comments:

submit

app/views/comments/_comment.html.erb

<%= render :partial => “comments/comment” %>

Rendering Partial Collections

Page 25: Getting started with Rails (3), Season 2

ROR Lab.

Refactoring

<h2>Comments</h2><% @post.comments.each do |comment| %>  <p>    <b>Commenter:</b>    <%= comment.commenter %>  </p>   <p>    <b>Comment:</b>    <%= comment.body %>  </p><% end %>

/app/views/posts/show.html.erbA post

comments:

submit

app/views/comments/_comment.html.erb

<%= render :partial => “comments/comment” %><%= render @post.comments %>  

Rendering Partial Collections

Page 26: Getting started with Rails (3), Season 2

ROR Lab.

Refactoring

<h2>Comments</h2><% @post.comments.each do |comment| %>  <p>    <b>Commenter:</b>    <%= comment.commenter %>  </p>   <p>    <b>Comment:</b>    <%= comment.body %>  </p><% end %>

/app/views/posts/show.html.erbA post

comments:

submit

app/views/comments/_comment.html.erb

<%= render :partial => “comments/comment” %><%= render @post.comments %>  

Rendering Partial Collections

local variable,comment

Page 27: Getting started with Rails (3), Season 2

ROR Lab.

Refactoring

<%= 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 %>

/app/views/posts/show.html.erbA post

comments:

submit

app/views/comments/_form.html.erb

Rendering a Partial Form

Page 28: Getting started with Rails (3), Season 2

ROR Lab.

Refactoring

<%= 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 %>

/app/views/posts/show.html.erbA post

comments:

submit

app/views/comments/_form.html.erb

Rendering a Partial Form

Page 29: Getting started with Rails (3), Season 2

ROR Lab.

<p id="notice"><%= notice %></p> <p>  <b>Name:</b>  <%= @post.name %></p> <p>  <b>Title:</b>  <%= @post.title %></p> <p>  <b>Content:</b>  <%= @post.content %></p> <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 %> |

A post

comments:

submit

app/views/posts/show.html.erb

Page 30: Getting started with Rails (3), Season 2

ROR Lab.

<p id="notice"><%= notice %></p> <p>  <b>Name:</b>  <%= @post.name %></p> <p>  <b>Title:</b>  <%= @post.title %></p> <p>  <b>Content:</b>  <%= @post.content %></p> <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 %> |

A post

comments:

submit

app/views/posts/show.html.erb

Page 31: Getting started with Rails (3), Season 2

ROR Lab.

<p id="notice"><%= notice %></p> <p>  <b>Name:</b>  <%= @post.name %></p> <p>  <b>Title:</b>  <%= @post.title %></p> <p>  <b>Content:</b>  <%= @post.content %></p> <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 %> |

A post

comments:

submit

app/views/posts/show.html.erb

<h2>Comments</h2><% @post.comments.each do |comment| %><%= render “comments/comment” %><% end %>

or

<h2>Comments</h2><%= render @post.comments %>

<h2>Add a comment:</h2><%= render "comments/form" %>

Page 32: Getting started with Rails (3), Season 2

ROR Lab.

Deleting Comments

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

app/views/comments/_comment.html.erb

DELETE /posts/:post_id/comments/:id

Page 33: Getting started with Rails (3), Season 2

ROR Lab.

Deleting Comments

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

app/views/comments/_comment.html.erb

DELETE /posts/:post_id/comments/:id

Page 34: Getting started with Rails (3), Season 2

ROR Lab.

Deleting Comments

<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

Page 35: Getting started with Rails (3), Season 2

ROR Lab.

Deleting Comments

<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

Page 36: Getting started with Rails (3), Season 2

ROR Lab.

Deleting Comments

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

DELETE /posts/:post_id/comments/:id

$ rake routes

Page 37: Getting started with Rails (3), Season 2

ROR Lab.

Deleting Comments

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

DELETE /posts/:post_id/comments/:id

$ rake routes

Page 38: Getting started with Rails (3), Season 2

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

Page 39: Getting started with Rails (3), Season 2

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

Page 40: Getting started with Rails (3), Season 2

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

Page 41: Getting started with Rails (3), Season 2

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

:dependent => :destroy:dependent => :delete:dependent => :nullify

Other options:

Page 42: Getting started with Rails (3), Season 2

ROR Lab.

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|

Page 43: Getting started with Rails (3), Season 2

ROR Lab.

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|

Page 44: Getting started with Rails (3), Season 2

ROR Lab.

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])

Page 45: Getting started with Rails (3), Season 2

ROR Lab.

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])

Page 46: Getting started with Rails (3), Season 2

ROR Lab.

SecurityA very simple HTTP authentication system

Page 47: Getting started with Rails (3), Season 2

ROR Lab.

Live DemoCreating a project ~ First model, Post

Page 48: Getting started with Rails (3), Season 2

ROR Lab.

감사합니다.����������� ������������������