Ruby on rails rails中的自定义路由
我有一个用户控制器 对于一个特定的用户,我想要这样的东西Ruby on rails rails中的自定义路由,ruby-on-rails,ruby-on-rails-3,routes,custom-routes,Ruby On Rails,Ruby On Rails 3,Routes,Custom Routes,我有一个用户控制器 对于一个特定的用户,我想要这样的东西 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_str
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
应该相同。您可以在config/routes.rb中创建重定向路由:
match '/a_constant_string', :to => redirect("/users/2")
它将重定向到正确的路径,并为您提供URL和路径帮助程序:
a_常量_字符串_路径
和a_常量_字符串_URL
您可以在config/routes.rb中创建重定向路由:
match '/a_constant_string', :to => redirect("/users/2")
它将重定向到正确的路径,并为您提供URL和路径帮助程序:a_常量_字符串_路径
和a_常量_字符串_URL
这也可以:
match '/a_constant_string', {controller: 'users', id: 2}
还有一个好处(IMO)就是不发送浏览器重定向。这也可以:
match '/a_constant_string', {controller: 'users', id: 2}
还有一个好处(IMO),就是不发送浏览器重定向