1

我有一个用户控制器。

对于特定用户,我想要这样的东西

 example.com/a_constant_string ==> example.com/users/2

我只需要特定用户,而不是所有用户。你可以说

link_to 'Go to a specific user', @user
link_to 'Go to a specific user', user_path(@user)
link_to 'Go to a specific user', a_constant_string_path

应该是一样的。

4

2 回答 2

6

这也可以:

match '/a_constant_string', {controller: 'users', id: 2}

具有不发送浏览器重定向的额外好处(IMO)。

于 2012-03-30T19:02:41.427 回答
4

您可以在 config/routes.rb 中创建重定向路由:

match '/a_constant_string', :to => redirect("/users/2")

这将重定向到正确的路径,并为您提供 URL 和 PATH 助手:a_constant_string_patha_constant_string_url.

于 2012-03-30T18:42:19.957 回答