Path routes and helpers are automatically created when a resource is declared in the config/routes.rb file. For example, if the resource name is items, this statement creates the routes:
map.resources :items
Generating a scaffold with an Item model (i.e. "ruby script/generate scaffold Item name:string ..."), automatically decares the items resouce in the routes.rb file. The table below shows the various item-based names for the different components. The bottom row shows an example consisting of two words. Generally, class names (including the Model class and the Controller class) are camel-cased (e.g. RestaurantReviews) while all other multi-word names are connected by underscores (restaurant_reviews), including pathnames, filenames and database tables.
| Resource name | Database table name | Model name | Controller name | View folder name |
|---|---|---|---|---|
| items | items | Item | items controller | items |
| people | people | Person | people controller | people |
| restaurant_reviews | restaurant_reviews | RestaurantReview | restaurant_reviews controller | restaurant_reviews |
This table shows the routes and helpers that are created by clearing items as a resource.
| Operation | CRUD term | View helper for creating request | HTTP term | Resulting path | Controller Method |
|---|---|---|---|---|---|
| Get list of resource items | Read | link_to "Items", items_path | GET | /items | index |
| Get a specific item | Read | link_to "Item", item_path(@item) OR link_to "Item", @item |
GET | /items/1 | show |
| Get form for creating new item | Read | link_to "Create New Item", new_item_path | GET | /items/new | new |
| Create new item | Create | form_for(@item) | POST | /items | create |
| Get completed form for modifying an existing item | Read | link_to "Update Item", edit_item_path(@item) | GET | /items/1/edit | edit |
| Update existing item | Update | form_for(@item) | PUT (HTML form uses POST) | /items/1 | update |
| Delete existing item | Destroy | link_to 'Destroy', @item, :confirm => 'Are you sure?', :method => :delete | DELETE (HTML link creates POST form) | /items/1 | destroy |
For the references to @item, it is assumed that this
references an item object in the controller.