I’m designing a minimalistic wiki in RoR. Basically a project have many pages. My routing file looks like this:
map.root :controller => 'projects' map.resources :projects, :has_many => :pages map.connect ':id', :controller => 'projects', :action => 'show' map.connect ':project_id/:id', :controller => 'pages', :action => 'show' map.connect ':controller/:action/:id' map.connect ':controller/:action/:id.:format'
This allow me to access, for example, the ‘main’ page of ‘teaching’ project like this:
http://localhost:3000/teaching/main
However, as soon as I click a link, it gets transformed to this:
http://localhost:3000/projects/teaching/pages/main
How can I make the helper methods that create URLs to stick to the scheme I want? I tried named routes, but I must be missing something out because it didn’t worked…
Rails is all about convention over configuration. You have to buy into the conventions if you want the convenience that Rails brings. I would strongly encourage sticking to the RESTful model and accept what rails is doing now.
With that said, you can probably hack something together. It won’t be pretty and it’ll be a pain every time you want to create a link.
So first get rid of
because the
map.resourcesis thing that is sending you to url you don’t want and the twomap.connect‘s don’t help you either. So now you should just haveThen when ever you want to create a link you are going to have to make it yourself. You are going to want to use something like this:
or something like that. I don’t know if that is exactly how are going to specify the route, you are probably going to have to change the :project_id and :id symbols.
Like I said you don’t want to take this route if you don’t have to. Stick to the conventions as much as you can because it’ll make your life much easier.