0

Say I have a User Model. And a route such as this:

http://www.mycoolapp.com/users/1

How can I match http://www.mycoolapp.com/1 where 1 matches the first User, without using /users/1.

Do you use a catch all? How is this accomplished in rails routing?

4

2 回答 2

1

假设您已经有一个用户资源,您的 Routes 文件将需要如下所示:

YourApp::Application.routes.draw do
  ....
  ....
  resources :user

  get ":id" => "users#show", :as => 'root_user'
end

最后一行 - 它必须是最后一行,因此它不会覆盖应用程序中的其他 url - 手动创建一个应该工作的路径。您还可以在代码中使用“root_user_path”来引用它来创建链接。

于 2013-06-24T03:12:08.013 回答
1

你可以这样做

get ":id" => "users#show"
于 2013-06-24T03:14:08.540 回答