21 Action Controller: Routing and URLs 20 ActiveRecord: Object Life Cycle
ruby -rubygems /home/rubys/git/rails/railties/bin/rails restful
create
create app/controllers
create app/helpers
create app/models
create app/views/layouts
create config/environments
create config/initializers
create config/locales
create db
create doc
create lib
create lib/tasks
create log
create public/images
create public/javascripts
create public/stylesheets
create script/performance
create test/fixtures
create test/functional
create test/integration
create test/performance
create test/unit
create vendor
create vendor/plugins
create tmp/sessions
create tmp/sockets
create tmp/cache
create tmp/pids
create Rakefile
create README
create app/controllers/application_controller.rb
create app/helpers/application_helper.rb
create config/database.yml
create config/routes.rb
create config/locales/en.yml
create db/seeds.rb
create config/initializers/backtrace_silencers.rb
create config/initializers/inflections.rb
create config/initializers/mime_types.rb
create config/initializers/new_rails_defaults.rb
create config/initializers/session_store.rb
create config/initializers/cookie_verification_secret.rb
create config/environment.rb
create config/boot.rb
create config/environments/production.rb
create config/environments/development.rb
create config/environments/test.rb
create script/about
create script/console
create script/dbconsole
create script/destroy
create script/generate
create script/runner
create script/server
create script/plugin
create script/performance/benchmarker
create script/performance/profiler
create test/test_helper.rb
create test/performance/browsing_test.rb
create public/404.html
create public/422.html
create public/500.html
create public/index.html
create public/favicon.ico
create public/robots.txt
create public/images/rails.png
create public/javascripts/prototype.js
create public/javascripts/effects.js
create public/javascripts/dragdrop.js
create public/javascripts/controls.js
create public/javascripts/application.js
create doc/README_FOR_APP
create log/server.log
create log/production.log
create log/development.log
create log/test.log
ruby script/generate scaffold article title:string summary:text content:text
exists app/models/
exists app/controllers/
exists app/helpers/
create app/views/articles
exists app/views/layouts/
exists test/functional/
exists test/unit/
create test/unit/helpers/
exists public/stylesheets/
create app/views/articles/index.html.erb
create app/views/articles/show.html.erb
create app/views/articles/new.html.erb
create app/views/articles/edit.html.erb
create app/views/layouts/articles.html.erb
create public/stylesheets/scaffold.css
create app/controllers/articles_controller.rb
create test/functional/articles_controller_test.rb
create app/helpers/articles_helper.rb
create test/unit/helpers/articles_helper_test.rb
route map.resources :articles
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/article.rb
create test/unit/article_test.rb
create test/fixtures/articles.yml
create db/migrate
create db/migrate/20100301051715_create_articles.rb
rake db:migrate
mv 20100301051715_create_articles.rb 20100301000001_create_articles.rb
(in /home/rubys/git/awdwr/work-188-235/restful)
== CreateArticles: migrating =================================================
-- create_table(:articles)
-> 0.0018s
== CreateArticles: migrated (0.0019s) ========================================
rake routes
(in /home/rubys/git/awdwr/work-188-235/restful)
articles GET /articles(.:format) {:controller=>"articles", :action=>"index"}
POST /articles(.:format) {:controller=>"articles", :action=>"create"}
new_article GET /articles/new(.:format) {:controller=>"articles", :action=>"new"}
edit_article GET /articles/:id/edit(.:format) {:controller=>"articles", :action=>"edit"}
article GET /articles/:id(.:format) {:controller=>"articles", :action=>"show"}
PUT /articles/:id(.:format) {:controller=>"articles", :action=>"update"}
DELETE /articles/:id(.:format) {:controller=>"articles", :action=>"destroy"}
/:controller/:action/:id
/:controller/:action/:id(.:format)
edit config/routes.rb
ActionController::Routing::Routes.draw do |map|
map.resources :articles
# ...
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
edit app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
# GET /articles
# GET /articles.xml
def index
@articles = Article.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @articles }
end
end
# GET /articles/1
# GET /articles/1.xml
def show
@article = Article.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @article }
end
end
# GET /articles/new
# GET /articles/new.xml
def new
@article = Article.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @article }
end
end
# GET /articles/1/edit
def edit
@article = Article.find(params[:id])
end
# POST /articles
# POST /articles.xml
def create
@article = Article.new(params[:article])
respond_to do |format|
if @article.save
format.html { redirect_to(@article, :notice => 'Article was successfully created.') }
format.xml { render :xml => @article, :status => :created,
:location => @article }
else
format.html { render :action => "new" }
format.xml { render :xml => @article.errors,
:status => :unprocessable_entity }
end
end
end
# PUT /articles/1
# PUT /articles/1.xml
def update
@article = Article.find(params[:id])
respond_to do |format|
if @article.update_attributes(params[:article])
format.html { redirect_to(@article, :notice => 'Article was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @article.errors,
:status => :unprocessable_entity }
end
end
end
# DELETE /articles/1
# DELETE /articles/1.xml
def destroy
@article = Article.find(params[:id])
@article.destroy
respond_to do |format|
format.html { redirect_to(articles_url) }
format.xml { head :ok }
end
end
end
edit app/views/articles/index.html.erb
<h1>Listing articles</h1>
<table>
<tr>
<th>Title</th>
<th>Summary</th>
<th>Content</th>
</tr>
<% @articles.each do |article| %>
<tr>
<td><%=h article.title %></td>
<td><%=h article.summary %></td>
<td><%=h article.content %></td>
<td><%= link_to 'Show', article %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article, :confirm => 'Are you sure?',
:method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New article', new_article_path %>
edit config/routes.rb
ActionController::Routing::Routes.draw do |map|
map.resources :articles, :collection => { :recent => :get }
# ...
# The priority is based upon order of creation: first created -> highest priority.
# Sample of regular route:
# map.connect 'products/:id', :controller => 'catalog', :action => 'view'
# Keep in mind you can assign values other than :controller and :action
# Sample of named route:
# map.purchase 'products/:id/purchase', :controller => 'catalog', :action => 'purchase'
# This route can be invoked with purchase_url(:id => product.id)
# Sample resource route (maps HTTP verbs to controller actions automatically):
# map.resources :products
# Sample resource route with options:
# map.resources :products, :member => { :short => :get, :toggle => :post }, :collection => { :sold => :get }
# Sample resource route with sub-resources:
# map.resources :products, :has_many => [ :comments, :sales ], :has_one => :seller
# Sample resource route with more complex sub-resources
# map.resources :products do |products|
# products.resources :comments
# products.resources :sales, :collection => { :recent => :get }
# end
# Sample resource route within a namespace:
# map.namespace :admin do |admin|
# # Directs /admin/products/* to Admin::ProductsController (app/controllers/admin/products_controller.rb)
# admin.resources :products
# end
# You can have the root of your site routed with map.root -- just remember to delete public/index.html.
# map.root :controller => "welcome"
# See how all your routes lay out with "rake routes"
# Install the default routes as the lowest priority.
# Note: These default routes make all actions in every controller accessible via GET requests. You should
# consider removing or commenting them out if you're using named routes and resources.
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
rake routes
(in /home/rubys/git/awdwr/work-188-235/restful)
recent_articles GET /articles/recent(.:format) {:controller=>"articles", :action=>"recent"}
articles GET /articles(.:format) {:controller=>"articles", :action=>"index"}
POST /articles(.:format) {:controller=>"articles", :action=>"create"}
new_article GET /articles/new(.:format) {:controller=>"articles", :action=>"new"}
edit_article GET /articles/:id/edit(.:format) {:controller=>"articles", :action=>"edit"}
article GET /articles/:id(.:format) {:controller=>"articles", :action=>"show"}
PUT /articles/:id(.:format) {:controller=>"articles", :action=>"update"}
DELETE /articles/:id(.:format) {:controller=>"articles", :action=>"destroy"}
/:controller/:action/:id
/:controller/:action/:id(.:format)
edit config/routes.rb
ActionController::Routing::Routes.draw do |map|
map.resources :articles, :member => { :embargo => :put, :release => :put }
# ...
# The priority is based upon order of creation: first created -> highest priority.
# Sample of regular route:
# map.connect 'products/:id', :controller => 'catalog', :action => 'view'
# Keep in mind you can assign values other than :controller and :action
# Sample of named route:
# map.purchase 'products/:id/purchase', :controller => 'catalog', :action => 'purchase'
# This route can be invoked with purchase_url(:id => product.id)
# Sample resource route (maps HTTP verbs to controller actions automatically):
# map.resources :products
# Sample resource route with options:
# map.resources :products, :member => { :short => :get, :toggle => :post }, :collection => { :sold => :get }
# Sample resource route with sub-resources:
# map.resources :products, :has_many => [ :comments, :sales ], :has_one => :seller
# Sample resource route with more complex sub-resources
# map.resources :products do |products|
# products.resources :comments
# products.resources :sales, :collection => { :recent => :get }
# end
# Sample resource route within a namespace:
# map.namespace :admin do |admin|
# # Directs /admin/products/* to Admin::ProductsController (app/controllers/admin/products_controller.rb)
# admin.resources :products
# end
# You can have the root of your site routed with map.root -- just remember to delete public/index.html.
# map.root :controller => "welcome"
# See how all your routes lay out with "rake routes"
# Install the default routes as the lowest priority.
# Note: These default routes make all actions in every controller accessible via GET requests. You should
# consider removing or commenting them out if you're using named routes and resources.
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
rake routes
(in /home/rubys/git/awdwr/work-188-235/restful)
articles GET /articles(.:format) {:controller=>"articles", :action=>"index"}
POST /articles(.:format) {:controller=>"articles", :action=>"create"}
new_article GET /articles/new(.:format) {:controller=>"articles", :action=>"new"}
release_article PUT /articles/:id/release(.:format) {:controller=>"articles", :action=>"release"}
embargo_article PUT /articles/:id/embargo(.:format) {:controller=>"articles", :action=>"embargo"}
edit_article GET /articles/:id/edit(.:format) {:controller=>"articles", :action=>"edit"}
article GET /articles/:id(.:format) {:controller=>"articles", :action=>"show"}
PUT /articles/:id(.:format) {:controller=>"articles", :action=>"update"}
DELETE /articles/:id(.:format) {:controller=>"articles", :action=>"destroy"}
/:controller/:action/:id
/:controller/:action/:id(.:format)
edit config/routes.rb
ActionController::Routing::Routes.draw do |map|
map.resources :articles, :new => { :shortform => :post }
# ...
# The priority is based upon order of creation: first created -> highest priority.
# Sample of regular route:
# map.connect 'products/:id', :controller => 'catalog', :action => 'view'
# Keep in mind you can assign values other than :controller and :action
# Sample of named route:
# map.purchase 'products/:id/purchase', :controller => 'catalog', :action => 'purchase'
# This route can be invoked with purchase_url(:id => product.id)
# Sample resource route (maps HTTP verbs to controller actions automatically):
# map.resources :products
# Sample resource route with options:
# map.resources :products, :member => { :short => :get, :toggle => :post }, :collection => { :sold => :get }
# Sample resource route with sub-resources:
# map.resources :products, :has_many => [ :comments, :sales ], :has_one => :seller
# Sample resource route with more complex sub-resources
# map.resources :products do |products|
# products.resources :comments
# products.resources :sales, :collection => { :recent => :get }
# end
# Sample resource route within a namespace:
# map.namespace :admin do |admin|
# # Directs /admin/products/* to Admin::ProductsController (app/controllers/admin/products_controller.rb)
# admin.resources :products
# end
# You can have the root of your site routed with map.root -- just remember to delete public/index.html.
# map.root :controller => "welcome"
# See how all your routes lay out with "rake routes"
# Install the default routes as the lowest priority.
# Note: These default routes make all actions in every controller accessible via GET requests. You should
# consider removing or commenting them out if you're using named routes and resources.
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
rake routes
(in /home/rubys/git/awdwr/work-188-235/restful)
articles GET /articles(.:format) {:controller=>"articles", :action=>"index"}
POST /articles(.:format) {:controller=>"articles", :action=>"create"}
shortform_new_article POST /articles/new/shortform(.:format) {:controller=>"articles", :action=>"shortform"}
new_article GET /articles/new(.:format) {:controller=>"articles", :action=>"new"}
edit_article GET /articles/:id/edit(.:format) {:controller=>"articles", :action=>"edit"}
article GET /articles/:id(.:format) {:controller=>"articles", :action=>"show"}
PUT /articles/:id(.:format) {:controller=>"articles", :action=>"update"}
DELETE /articles/:id(.:format) {:controller=>"articles", :action=>"destroy"}
/:controller/:action/:id
/:controller/:action/:id(.:format)
edit config/routes.rb
ActionController::Routing::Routes.draw do |map|
map.resources :articles do |article|
article.resources :comments
end
# ...
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
rake routes
(in /home/rubys/git/awdwr/work-188-235/restful)
article_comments GET /articles/:article_id/comments(.:format) {:controller=>"comments", :action=>"index"}
POST /articles/:article_id/comments(.:format) {:controller=>"comments", :action=>"create"}
new_article_comment GET /articles/:article_id/comments/new(.:format) {:controller=>"comments", :action=>"new"}
edit_article_comment GET /articles/:article_id/comments/:id/edit(.:format) {:controller=>"comments", :action=>"edit"}
article_comment GET /articles/:article_id/comments/:id(.:format) {:controller=>"comments", :action=>"show"}
PUT /articles/:article_id/comments/:id(.:format) {:controller=>"comments", :action=>"update"}
DELETE /articles/:article_id/comments/:id(.:format) {:controller=>"comments", :action=>"destroy"}
articles GET /articles(.:format) {:controller=>"articles", :action=>"index"}
POST /articles(.:format) {:controller=>"articles", :action=>"create"}
new_article GET /articles/new(.:format) {:controller=>"articles", :action=>"new"}
edit_article GET /articles/:id/edit(.:format) {:controller=>"articles", :action=>"edit"}
article GET /articles/:id(.:format) {:controller=>"articles", :action=>"show"}
PUT /articles/:id(.:format) {:controller=>"articles", :action=>"update"}
DELETE /articles/:id(.:format) {:controller=>"articles", :action=>"destroy"}
/:controller/:action/:id
/:controller/:action/:id(.:format)
ruby script/generate model comment comment:text article_id:integer
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/comment.rb
create test/unit/comment_test.rb
create test/fixtures/comments.yml
exists db/migrate
create db/migrate/20100301051730_create_comments.rb
ruby script/generate controller comments new edit update destroy
exists app/controllers/
exists app/helpers/
create app/views/comments
exists test/functional/
exists test/unit/helpers/
create app/controllers/comments_controller.rb
create test/functional/comments_controller_test.rb
create app/helpers/comments_helper.rb
create test/unit/helpers/comments_helper_test.rb
create app/views/comments/new.html.erb
create app/views/comments/edit.html.erb
create app/views/comments/update.html.erb
create app/views/comments/destroy.html.erb
rm app/views/comments/destroy.html.erb
rm app/views/comments/update.html.erb
edit app/models/article.rb
class Article < ActiveRecord::Base
has_many :comments
end
edit app/models/comment.rb
class Comment < ActiveRecord::Base
belongs_to :article
end
rake db:migrate
mv 20100301051730_create_comments.rb 20100301000002_create_comments.rb
(in /home/rubys/git/awdwr/work-188-235/restful)
== CreateComments: migrating =================================================
-- create_table(:comments)
-> 0.0018s
== CreateComments: migrated (0.0019s) ========================================
edit app/views/articles/show.html.erb
<p>
<b>Title:</b>
<%=h @article.title %>
</p>
<p>
<b>Summary:</b>
<%=h @article.summary %>
</p>
<p>
<b>Content:</b>
<%=h @article.content %>
</p>
<% unless @article.comments.empty? %>
<%= render :partial => "/comments/comment",
:collection => @article.comments %>
<% end %>
<%= link_to "Add comment", new_article_comment_url(@article) %> |
<%= link_to 'Edit', edit_article_path(@article) %> |
<%= link_to 'Back', articles_path %>
edit app/views/comments/_comment.html.erb
<div class="comment">
<p>
<strong>Comment last updated: <%= comment.updated_at %></strong>
<%= link_to "Edit", edit_article_comment_url(:id => comment,
:article_id => comment.article) %>
<%= link_to "Delete", article_comment_url(:id => comment,
:article_id => comment.article),
:method => :delete %>
</p>
<blockquote>
<%=h comment.comment %>
</blockquote>
</div>
edit app/views/comments/_form.html.erb
<%= form.text_area :comment, :rows => 3 %>
edit app/views/comments/edit.html.erb
<h1>Editing comment</h1>
<%= error_messages_for :comment %>
<% form_for([@article,@comment]) do |f| %>
<p>
<b>Comment</b><br />
<%= f.text_area :comment %>
</p>
<p>
<b>Article</b><br />
<%= f.text_field :article_id %>
</p>
<p>
<%= f.submit "Update" %>
</p>
<% end %>
<%= link_to 'Back', article_path(@article) %>
edit app/views/comments/new.html.erb
<% form_for [@article, @comment] do |form| %>
<fieldset>
<legend>Add a Comment</legend>
<%= render :partial => 'form', :object => form %>
<p>
<%= submit_tag "Create" %>
</p>
</fieldset>
<% end %>
edit app/controllers/comments_controller.rb
class CommentsController < ApplicationController
before_filter :find_article
def new
@comment = Comment.new
end
def edit
@comment = @article.comments.find(params[:id])
end
def create
@comment = Comment.new(params[:comment])
if (@article.comments << @comment)
redirect_to article_url(@article)
else
render :action => :new
end
end
def update
@comment = @article.comments.find(params[:id])
if @comment.update_attributes(params[:comment])
redirect_to article_url(@article)
else
render :action => :edit
end
end
def destroy
comment = @article.comments.find(params[:id])
@article.comments.delete(comment)
redirect_to article_url(@article)
end
private
def find_article
@article_id = params[:article_id]
return(redirect_to(articles_url)) unless @article_id
@article = Article.find(@article_id)
end
end
ruby -rubygems /home/rubys/git/rails/railties/bin/rails routing
create
create app/controllers
create app/helpers
create app/models
create app/views/layouts
create config/environments
create config/initializers
create config/locales
create db
create doc
create lib
create lib/tasks
create log
create public/images
create public/javascripts
create public/stylesheets
create script/performance
create test/fixtures
create test/functional
create test/integration
create test/performance
create test/unit
create vendor
create vendor/plugins
create tmp/sessions
create tmp/sockets
create tmp/cache
create tmp/pids
create Rakefile
create README
create app/controllers/application_controller.rb
create app/helpers/application_helper.rb
create config/database.yml
create config/routes.rb
create config/locales/en.yml
create db/seeds.rb
create config/initializers/backtrace_silencers.rb
create config/initializers/inflections.rb
create config/initializers/mime_types.rb
create config/initializers/new_rails_defaults.rb
create config/initializers/session_store.rb
create config/initializers/cookie_verification_secret.rb
create config/environment.rb
create config/boot.rb
create config/environments/production.rb
create config/environments/development.rb
create config/environments/test.rb
create script/about
create script/console
create script/dbconsole
create script/destroy
create script/generate
create script/runner
create script/server
create script/plugin
create script/performance/benchmarker
create script/performance/profiler
create test/test_helper.rb
create test/performance/browsing_test.rb
create public/404.html
create public/422.html
create public/500.html
create public/index.html
create public/favicon.ico
create public/robots.txt
create public/images/rails.png
create public/javascripts/prototype.js
create public/javascripts/effects.js
create public/javascripts/dragdrop.js
create public/javascripts/controls.js
create public/javascripts/application.js
create doc/README_FOR_APP
create log/server.log
create log/production.log
create log/development.log
create log/test.log
ruby script/generate controller store index add_to_cart
exists app/controllers/
exists app/helpers/
create app/views/store
exists test/functional/
create test/unit/helpers/
create app/controllers/store_controller.rb
create test/functional/store_controller_test.rb
create app/helpers/store_helper.rb
create test/unit/helpers/store_helper_test.rb
create app/views/store/index.html.erb
create app/views/store/add_to_cart.html.erb
cp -v /home/rubys/git/awdwr/data/routing/* config
`/home/rubys/git/awdwr/data/routing/routes.rb' -> `config/routes.rb'
`/home/rubys/git/awdwr/data/routing/routes_with_conditions.rb' -> `config/routes_with_conditions.rb'
`/home/rubys/git/awdwr/data/routing/routes_with_names.rb' -> `config/routes_with_names.rb'
`/home/rubys/git/awdwr/data/routing/routing_conditions_test.rb' -> `config/routing_conditions_test.rb'
`/home/rubys/git/awdwr/data/routing/routing_test.rb' -> `config/routing_test.rb'
mv -v config/*_test.rb test/unit
`config/routing_conditions_test.rb' -> `test/unit/routing_conditions_test.rb'
`config/routing_test.rb' -> `test/unit/routing_test.rb'
rake db:schema:dump
(in /home/rubys/git/awdwr/work-188-235/routing)
rake test
/home/rubys/.rvm/rubies/ruby-1.8.8-r26786/bin/ruby -I"lib:test" "/home/rubys/.rvm/gems/ruby-1.8.8-r26786%global/gems/rake-0.8.7/lib/rake/rake_test_loader.rb" "test/unit/helpers/store_helper_test.rb" "test/unit/routing_conditions_test.rb" "test/unit/routing_test.rb"
(in /home/rubys/git/awdwr/work-188-235/routing)
Loaded suite /home/rubys/.rvm/gems/ruby-1.8.8-r26786%global/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
.....
Finished in 0.053208 seconds.
5 tests, 29 assertions, 0 failures, 0 errors
/home/rubys/.rvm/rubies/ruby-1.8.8-r26786/bin/ruby -I"lib:test" "/home/rubys/.rvm/gems/ruby-1.8.8-r26786%global/gems/rake-0.8.7/lib/rake/rake_test_loader.rb" "test/functional/store_controller_test.rb"
Loaded suite /home/rubys/.rvm/gems/ruby-1.8.8-r26786%global/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
.
Finished in 0.023072 seconds.
1 tests, 1 assertions, 0 failures, 0 errors
/home/rubys/.rvm/rubies/ruby-1.8.8-r26786/bin/ruby -I"lib:test" "/home/rubys/.rvm/gems/ruby-1.8.8-r26786%global/gems/rake-0.8.7/lib/rake/rake_test_loader.rb"
21 Action Controller: Routing and URLs 20 ActiveRecord: Object Life Cycle