Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails: How do you prefix named routes?

I'm looking to generate a link that has a prefix attached to the named route itself. Something like this to display the path "/old/recipes":

recipes_path(:prefix => "old/")  # the correct way should show "/old/recipes"

I don't want to touch the routes.rb file, but modify the named route with the prefix attached. Is that possible and how would you do it correctly?

EDIT: I'm using Rails 3. The reason for adding the optional prefix is I want to use the normal recipes_path as well. So I want to use both "/recipes" and "/old/recipes".

like image 938
sjsc Avatar asked Dec 17 '10 03:12

sjsc


1 Answers

You're going to run into a lot of pain if you don't want to touch the routes file, mainly because that's what Rails is going to reference when it tries to figure out where your route goes. I don't know of another way to do that, so here's the config/routes.rb code when you're convinced it's a good idea:

scope :path => "old" do
  resources :recipes
end

Now when you route to recipes_path it will go to /old/recipes, although this may not be what you're after. If that's the case, then you may want to chuck the as option on the end of this scope:

scope :path => "old", :as => "old" do
  resources :recipes
end

In which case, this route would now be old_recipes_path, still routing to /old/recipes.

like image 88
Ryan Bigg Avatar answered Oct 15 '22 18:10

Ryan Bigg