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