Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. Instead of declaring separate routes for your index, show, new, edit, create, update and destroy actions, a resourceful route declares them in a single line of code:
resources :photos
Sometimes, you have a resource that clients always look up without referencing an ID. A common example, /profile always shows the profile of the currently logged in user. In this case, you can use a singular resource to map /profile (rather than /profile/:id) to the show action.
resource :profile
It’s common to have resources that are logically children of other resources:
resources :magazines do resources :ads end
You may wish to organize groups of controllers under a namespace. Most commonly, you might group a number of administrative controllers under an admin namespace. You would place these controllers under the app/controllers/admin directory, and you can group them together in your router:
namespace "admin" do resources :posts, :comments end
By default the :id parameter doesn’t accept dots. If you need to use dots as part of the :id parameter add a constraint which overrides this restriction, e.g:
resources :articles, :id => /[^\/]+/
This allows any character other than a slash as part of your :id.
- C
- M
- N
- R
- S
- W
- CLASS ActionDispatch::Routing::Mapper::Resources::Resource
- CLASS ActionDispatch::Routing::Mapper::Resources::SingletonResource
VALID_ON_OPTIONS | = | [:new, :collection, :member] |
CANONICAL_ACTIONS holds all actions that does not need a prefix or a path appended since they fit properly in their scope level. |
||
RESOURCE_OPTIONS | = | [:as, :controller, :path, :only, :except] |
CANONICAL_ACTIONS | = | %w(index create new show update destroy) |
To add a route to the collection:
resources :photos do collection do get 'search' end end
This will enable Rails to recognize paths such as /photos/search with GET, and route to the search action of PhotosController. It will also create the search_photos_url and search_photos_path route helpers.
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1164 1164: def collection 1165: unless resource_scope? 1166: raise ArgumentError, "can't use collection outside resource(s) scope" 1167: end 1168: 1169: with_scope_level(:collection) do 1170: scope(parent_resource.collection_scope) do 1171: yield 1172: end 1173: end 1174: end
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1252 1252: def match(path, *rest) 1253: if rest.empty? && Hash === path 1254: options = path 1255: path, to = options.find { |name, value| name.is_a?(String) } 1256: options[:to] = to 1257: options.delete(path) 1258: paths = [path] 1259: else 1260: options = rest.pop || {} 1261: paths = [path] + rest 1262: end 1263: 1264: options[:anchor] = true unless options.key?(:anchor) 1265: 1266: if options[:on] && !VALID_ON_OPTIONS.include?(options[:on]) 1267: raise ArgumentError, "Unknown scope #{on.inspect} given to :on" 1268: end 1269: 1270: paths.each { |_path| decomposed_match(_path, options.dup) } 1271: self 1272: end
To add a member route, add a member block into the resource block:
resources :photos do member do get 'preview' end end
This will recognize /photos/1/preview with GET, and route to the preview action of PhotosController. It will also create the preview_photo_url and preview_photo_path helpers.
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1187 1187: def member 1188: unless resource_scope? 1189: raise ArgumentError, "can't use member outside resource(s) scope" 1190: end 1191: 1192: with_scope_level(:member) do 1193: scope(parent_resource.member_scope) do 1194: yield 1195: end 1196: end 1197: end
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1211 1211: def nested 1212: unless resource_scope? 1213: raise ArgumentError, "can't use nested outside resource(s) scope" 1214: end 1215: 1216: with_scope_level(:nested) do 1217: if shallow? 1218: with_exclusive_scope do 1219: if @scope[:shallow_path].blank? 1220: scope(parent_resource.nested_scope, nested_options) { yield } 1221: else 1222: scope(@scope[:shallow_path], :as => @scope[:shallow_prefix]) do 1223: scope(parent_resource.nested_scope, nested_options) { yield } 1224: end 1225: end 1226: end 1227: else 1228: scope(parent_resource.nested_scope, nested_options) { yield } 1229: end 1230: end 1231: end
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1199 1199: def new 1200: unless resource_scope? 1201: raise ArgumentError, "can't use new outside resource(s) scope" 1202: end 1203: 1204: with_scope_level(:new) do 1205: scope(parent_resource.new_scope(action_path(:new))) do 1206: yield 1207: end 1208: end 1209: end
Sometimes, you have a resource that clients always look up without referencing an ID. A common example, /profile always shows the profile of the currently logged in user. In this case, you can use a singular resource to map /profile (rather than /profile/:id) to the show action:
resource :geocoder
creates six different routes in your application, all mapping to the GeoCoders controller (note that the controller is named after the plural):
GET /geocoder/new POST /geocoder GET /geocoder GET /geocoder/edit PUT /geocoder DELETE /geocoder
Options
Takes same options as resources.
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 984 984: def resource(*resources, &block) 985: options = resources.extract_options! 986: 987: if apply_common_behavior_for(:resource, resources, options, &block) 988: return self 989: end 990: 991: resource_scope(:resource, SingletonResource.new(resources.pop, options)) do 992: yield if block_given? 993: 994: collection do 995: post :create 996: end if parent_resource.actions.include?(:create) 997: 998: new do 999: get :new 1000: end if parent_resource.actions.include?(:new) 1001: 1002: member do 1003: get :edit if parent_resource.actions.include?(:edit) 1004: get :show if parent_resource.actions.include?(:show) 1005: put :update if parent_resource.actions.include?(:update) 1006: delete :destroy if parent_resource.actions.include?(:destroy) 1007: end 1008: end 1009: 1010: self 1011: end
In Rails, a resourceful route provides a mapping between HTTP verbs and URLs and controller actions. By convention, each action also maps to particular CRUD operations in a database. A single entry in the routing file, such as
resources :photos
creates seven different routes in your application, all mapping to the Photos controller:
GET /photos GET /photos/new POST /photos GET /photos/:id GET /photos/:id/edit PUT /photos/:id DELETE /photos/:id
Resources can also be nested infinitely by using this block syntax:
resources :photos do resources :comments end
This generates the following comments routes:
GET /photos/:photo_id/comments GET /photos/:photo_id/comments/new POST /photos/:photo_id/comments GET /photos/:photo_id/comments/:id GET /photos/:photo_id/comments/:id/edit PUT /photos/:photo_id/comments/:id DELETE /photos/:photo_id/comments/:id
Options
Takes same options as Base#match as well as:
- :path_names
- Allows you to change the segment component of the edit and
new actions. Actions not specified are not changed.
resources :posts, :path_names => { :new => "brand_new" }
The above example will now change /posts/new to /posts/brand_new
- :path
- Allows you to change the path prefix for the resource.
resources :posts, :path => 'postings'
The resource and all segments will now route to /postings instead of /posts
- :only
- Only generate routes for the given actions.
resources :cows, :only => :show resources :cows, :only => [:show, :index]
- :except
- Generate all routes except for the given actions.
resources :cows, :except => :show resources :cows, :except => [:show, :index]
- :shallow
- Generates shallow routes for nested resource(s). When placed on a parent
resource, generates shallow routes for all nested resources.
resources :posts, :shallow => true do resources :comments end
Is the same as:
resources :posts do resources :comments, :except => [:show, :edit, :update, :destroy] end resources :comments, :only => [:show, :edit, :update, :destroy]
This allows URLs for resources that otherwise would be deeply nested such as a comment on a blog post like /posts/a-long-permalink/comments/1234 to be shortened to just /comments/1234.
- :shallow_path
- Prefixes nested shallow routes with the specified path.
scope :shallow_path => "sekret" do resources :posts do resources :comments, :shallow => true end end
The comments resource here will have the following routes generated for it:
post_comments GET /posts/:post_id/comments(.:format) post_comments POST /posts/:post_id/comments(.:format) new_post_comment GET /posts/:post_id/comments/new(.:format) edit_comment GET /sekret/comments/:id/edit(.:format) comment GET /sekret/comments/:id(.:format) comment PUT /sekret/comments/:id(.:format) comment DELETE /sekret/comments/:id(.:format)
Examples
# routes call <tt>Admin::PostsController</tt> resources :posts, :module => "admin" # resource actions are at /admin/posts. resources :posts, :path => "admin/posts"
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1122 1122: def resources(*resources, &block) 1123: options = resources.extract_options! 1124: 1125: if apply_common_behavior_for(:resources, resources, options, &block) 1126: return self 1127: end 1128: 1129: resource_scope(:resources, Resource.new(resources.pop, options)) do 1130: yield if block_given? 1131: 1132: collection do 1133: get :index if parent_resource.actions.include?(:index) 1134: post :create if parent_resource.actions.include?(:create) 1135: end 1136: 1137: new do 1138: get :new 1139: end if parent_resource.actions.include?(:new) 1140: 1141: member do 1142: get :edit if parent_resource.actions.include?(:edit) 1143: get :show if parent_resource.actions.include?(:show) 1144: put :update if parent_resource.actions.include?(:update) 1145: delete :destroy if parent_resource.actions.include?(:destroy) 1146: end 1147: end 1148: 1149: self 1150: end
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1309 1309: def root(options={}) 1310: if @scope[:scope_level] == :resources 1311: with_scope_level(:root) do 1312: scope(parent_resource.path) do 1313: super(options) 1314: end 1315: end 1316: else 1317: super(options) 1318: end 1319: end
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1377 1377: def with_exclusive_scope 1378: begin 1379: old_name_prefix, old_path = @scope[:as], @scope[:path] 1380: @scope[:as], @scope[:path] = nil, nil 1381: 1382: with_scope_level(:exclusive) do 1383: yield 1384: end 1385: ensure 1386: @scope[:as], @scope[:path] = old_name_prefix, old_path 1387: end 1388: end
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1390 1390: def with_scope_level(kind, resource = parent_resource) 1391: old, @scope[:scope_level] = @scope[:scope_level], kind 1392: old_resource, @scope[:scope_level_resource] = @scope[:scope_level_resource], resource 1393: yield 1394: ensure 1395: @scope[:scope_level] = old 1396: @scope[:scope_level_resource] = old_resource 1397: end