如果存在参数,则Rails不同的路由

时间:2012-10-02 16:22:22

标签: ruby-on-rails routes

小路由问题:

我想要

  • myapp.com/my_controller - >通向控制器A
  • myapp.com/my_controller?uid=123 - >通向控制器B

如何更改路径文件的任何想法(在rails 2.3中)

1 个答案:

答案 0 :(得分:1)

Rails 2.3.1:

map.route_a 'my_controller', :controller => "A", :action => "a"
map.route_b 'my_controller/uid/:uid', :controller => "B", :action => "b"

您应该获得route_a_urlroute_b_url(:uid => uid)辅助方法来生成网址。它不完全是查询参数,而是使用uid和值两者。

Rails 3.2.1:

match 'my_controller' => 'A#a', :as => 'route_a'
match 'my_controller/uid/:uid' => 'B#b', :as => 'route_b'

辅助函数route_a_urlroute_b_url(:uid=>10)随时可用。

<强> Explnation:


在您的视图中,使用辅助函数生成网址

助手功能: route_a_url()
生成的网址 http://localhost:3000/my_controller
映射到: Controller A, Action a

助手功能: route_b_url(:uid => 10))
生成的网址 http://localhost:3000/my_controller/uid/10
映射到: Controller B, Action b