Rails 3 Controller: Difference between revisions
Jump to navigation
Jump to search
Line 37: | Line 37: | ||
If you generate a scaffold, you will get all seven of the default actions for RESTful controllers: | If you generate a scaffold, you will get all seven of the default actions for RESTful controllers: | ||
index, show, new, edit, create, update, and destroy | index, show, new, edit, create, update, and destroy | ||
== Controllers == | |||
=== ApplicationController === | === ApplicationController === | ||
Every controller is a subclass of <code>ApplicationController</code>. Any methods or data in <code>ApplicationController</code> is therefore available to all other controllers. | Every controller is a subclass of <code>ApplicationController</code>. Any methods or data in <code>ApplicationController</code> is therefore available to all other controllers. |
Revision as of 22:19, 30 June 2011
Routes
defined in config/routes.rb
:
match ':controller(/:action(/:id(.:format)))' # default route, :id and :format may be accessed as parameters in the action
match '/teams/home' => 'teams#index' # call index action of teams controller
match '/teams/search/:query' => 'teams#search' # sends :query parameter to search action in teams controller
match '/teams/search/:query' => 'teams#search', :as => 'search' # named route, defines search_url and search_path methods
search_url => http://example.com/teams/search search_path => /teams/search
useful in something like
link_to "Search", search_path
root url
use something like this in config/routes.rb
:
root :to => "articles#index"
NOTE: You must also delete /public/index.html
to make sure the web server doesn't bypass Rails.
RESTful Routes and Resources
If you add
resources :articles
to config/routes.rb
, the following named routes are automatically created:
article_path => /articles/:id articles_path => /articles edit_article_path => /articles/edit/:id new_article_path => /articles/new
If you generate a scaffold, you will get all seven of the default actions for RESTful controllers: index, show, new, edit, create, update, and destroy
Controllers
ApplicationController
Every controller is a subclass of ApplicationController
. Any methods or data in ApplicationController
is therefore available to all other controllers.