How do we identify parameters in a dynamic URL?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 11:53:30

You can use route globbing and constraints to match a proper pattern.

# Rails 2.x
map.connect "*path/:year/:month", 
             :constraints => {:year => /\d{4}/, :month => /0[1-9]|1[0-2]/ },
             :controller => :pages, :action => :month_archive

# Rails 3.x
match "*path/:year/:month" => "pages#month_archive", 
             :constraints => {:year => /\d{4}/, :month => /0[1-9]|1[0-2]/ }

This will match /dogs/snoopy-news/2010/11 and pass :path => "dogs/snoopy-news", :year => "2010", :month => "11" in the params hash. It will match all routes that have a year and a month as the last two segments, regardless of how many segments come beforehand. And it will reject any route that doesn't match a proper year and month in the last two segments. What you do with the :path parameter is up to you in the controller.

You can configure this issue in your routes.rb file.

you can add a route like

 map.connect 'snoopy-news/:year/:date', :controller => 'needed controller', :action => 'needed action' 

this will route any URL of the format

 ../snoopy-news/2010/23 
to the corresponding controller and action with the values set in the varialbles
 year , date 

If you dont give anything, RAILS will consider this as a parameter.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!