views:

132

answers:

2

Hi everyone,

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...

+1  A: 

I would ditch the map.connect stuff - it's not very RESTful, and can get you very confused.

At first glance I thought you could use the :member and :collection directives to add in what you wanted, but when I look more closely I realised it won't help.

I'm having a little difficulty understanding your data model - a page can have many pages? Or is teaching the project name and then it has pages?

If that is the case, then you probably need to look at a plugin like SubDomainFu and use subdomains based on project names, rather than hacking the routes file directly. We have used this successfully to give a scheme like you describe (the domain implies the project, bit of extra code required) and also things like teaching.yourdomain.com and learning.yourdomain.com (which can be fun if you want to use SSL, but that's a different story).

Ghoti
'Teaching' is the name of the project, and yes, I'm trying to address based on names instead of ids. I'll take a look at SubDomainFu.
Hugo S Ferreira
A: 

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

 map.resources :projects, :has_many => :pages
 map.connect ':controller/:action/:id'
 map.connect ':controller/:action/:id.:format'

because the map.resources is thing that is sending you to url you don't want and the two map.connect's don't help you either. So now you should just have

 map.connect ':project_id/:id', :controller => 'pages', :action => 'show'
 map.connect ':id', :controller => 'projects', :action => 'show'

Then 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:

 <%= link_to 'Blah', :project_id => @project, :id => @project.pages.first %>

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.

vrish88