Tuesday, January 31, 2012

dynamic & bounded parameters and named routes

by Sandip Ransing 0 comments

Rails routes can be customized as your own routes with parameters but first you should understand how routes behaves.
Adding dynamic parameters to routes
Here exact parameters are matched to route and presence of each parameter is mandatory in order to construct urls. blank parameter will raise RoutingError exception.
Exact matched named route declared as - match ':a/:b/:c', :to => 'home#index', :as => :q now go to the rails console - ruby-1.9.3-head :005 > app.q_url(:a, :b, :c) => "http://www.example.com/a/b/c" ruby-1.9.3-head :006 > app.q_url(:a, :b, '') ActionController::RoutingError: No route matches {:controller=>"home", :a=>:a, :b=>:b, :c=>""} Bound parameters to named routes
If you are too sure that certain parameter can be blank then you can define it as optional parameter inside route -
match ':a/:b(/:c)', :to => 'home#index', :as => :q rails console ruby-1.9.3-head :010 > app.q_url(:a, :b, '') => "http://www.example.com/a/b?c=" ruby-1.9.3-head :011 > app.q_url(:a, :b) => "http://www.example.com/a/b"

blog comments powered by Disqus

About The Author

Sandip is a ruby on rails developer based in pune and also a blogger at funonrails. Opensource contributor and working with Josh software Private Limited. for more info read Follow Sandip on Twitter for updates.

Connect With Me...

Github Projects

@sandipransing Twitter