1

I have a users controller.

For a specific user I want to have something like this

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

I just need for a specific user, not for all users. You can say

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

Should be same.

Mohit Jain
  • 43,139
  • 57
  • 169
  • 274

2 Answers2

6

This would work too:

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

with the added benefit (IMO) of not sending a browser redirect.

James
  • 4,797
  • 25
  • 29
dsetton
  • 816
  • 8
  • 15
4

You could create a redirect route in config/routes.rb:

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

Which would redirect to the correct path, and gives you the URL and PATH helpers: a_constant_string_path and a_constant_string_url.

ipd
  • 5,674
  • 3
  • 34
  • 49