diff options
author | Julio Capote <jcapote@gmail.com> | 2018-11-06 03:03:41 +0000 |
---|---|---|
committer | Julio Capote <jcapote@gmail.com> | 2018-11-06 03:03:41 +0000 |
commit | 4b489a049a0063bbb1fd9f0c0f74ce1ee9f87a86 (patch) | |
tree | 98af5707e30150af482e297bed9cd4e9b5477e6a /content/post/2009-1-1-useful-rails-routing-tips.markdown | |
parent | a62a3e7755579d93ce3a87243dd277575930fffe (diff) | |
download | capotej.com-4b489a049a0063bbb1fd9f0c0f74ce1ee9f87a86.tar.gz |
import old posts
Diffstat (limited to 'content/post/2009-1-1-useful-rails-routing-tips.markdown')
-rw-r--r-- | content/post/2009-1-1-useful-rails-routing-tips.markdown | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/content/post/2009-1-1-useful-rails-routing-tips.markdown b/content/post/2009-1-1-useful-rails-routing-tips.markdown new file mode 100644 index 0000000..ef6525e --- /dev/null +++ b/content/post/2009-1-1-useful-rails-routing-tips.markdown @@ -0,0 +1,75 @@ +--- +layout: post +title: "Useful Rails Routing tips" +date: 2009-01-02T15:50:00Z +comments: false +permalink: /post/67873462/useful-rails-routing-tips +categories: +--- + + + +Even though I have been using Rails for fun and profit for about 2 years now, I felt I never really used it’s routing engine to its full potential. So I checked out new [Rails Routing from the outside in](http://guides.rubyonrails.org/routing_outside_in.html) guide and discovered bunch of useful tricks that I (and maybe you) had no idea you could do. Here they are: + +### Multiple resource definitions on a single line + +```ruby +map.resources :photos, :books, :videos +``` + +### Impose a certain format for resource identifiers + +```ruby +map.resources :photos, :requirements => { :id => /[A-Z][A-Z][0-9]+/ } +``` + +This way, `/photos/3` would not work, but `/photos/DA321` would. + +### Friendlier action names + +Say for your application ‘create’ and ‘change’ make more sense than the default ‘new’ and ‘edit’ you can do + +```ruby +map.resources :photos, :path_names => { :new => 'make', :edit => 'change' } +``` + +You can also do this site-wide also, in your environment.rb + +```ruby +config.action_controller.resources_path_names = { :new => 'make', :edit => 'change' } +``` + +### Trim the fat off resources with :only and :except + + +When you use map.resources, rails generates 7 restful routes for that resource; But what if that resource only needed to be seen and listed, never edited or created? + +```ruby +map.resources :photos, :only => [:index, :show] +``` + +If your application uses a lot of `map.resources` calls but not neccesarily all its generated routes, you can save memory this way. + +### Adding extra routes to your resources + +Instead of fighting the `map.resources` generator by placing a horror like this atop your routes.rb + +```ruby +map.connect '/photos/:id/preview', { :controller => 'photos', :action => 'preview' } +``` + +You can do this to your already mapped resource + +```ruby +map.resources :photos, :member => { :preview => :get } +``` + +This will map all GET’s to `/photos/3` to the preview action of your photos controller + +This can also be used in collections instead of singular members, just change `:member` to `:collection` + +```ruby +map.resources :photos, :collection => { :search => :get } +``` + +This will give you `/photos/search` and hit the search action within the photos controller |