Getting Started with Rails (3) - The 3rd Round - July 21, 2012 Hyoseong Choi ROR Lab. ROR Lab. Season 2
Getting Started with Rails (3)
- The 3rd Round -
July 21, 2012
Hyoseong ChoiROR Lab.
ROR Lab. 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
ROR Lab.
Addinga Second Model
Comment
Class
singularcomments
plural
Database
ORM
ROR Lab.
Addinga Second Model
Comment
Model Class
comments
Database Table
ActiveRecord
object recordattributes fields
ROR Lab.
Generating a Model
$ rails generate model Comment commenter:string body:text post:references
generate scaffoldgenerate modelgenerate controllergenerate migration
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
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
ROR Lab.
Associating Models
Post Comment
Parent Model Child Model
has_many
belongs_to
app/models/post.rb app/models/comment.rb
Relation
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
ROR Lab.
Adding a Route
resources :posts do resources :commentsend
config/routes.rb
ROR Lab.
Generating a Controller
$ rails generate controller Comments
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
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
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
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
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
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
ROR Lab.
Refactoring
• Getting long and awkward
• Using “partials” to clean up
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
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
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
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
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
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
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
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
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
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
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
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
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" %>
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
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
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
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
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
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
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
:dependent => :destroy:dependent => :delete:dependent => :nullify
Other options:
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|
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|
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])
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])
ROR Lab.
SecurityA very simple HTTP authentication system
ROR Lab.
Live DemoCreating a project ~ First model, Post
ROR Lab.
감사합니다.����������� ������������������