Namespaced and nested controllers/resouces

Hello,

My controllers: - ForumsController - Forum::TopicsController - Forum::Topic::PostsController

Resources:

map.resources :forums do |forums|   forums.resources :topics, :controller => "forum/topics" do |topics|     topics.resources :posts, :controller => "forum/topic/posts"   end end

Is it a good design? Problem is that only way to get a post path is to call:

forum_topic_post_path(post.topic.forum, post.topic, post)

There’s no need to have namespaces in your controllers

  • ForumsController

  • Forum::TopicsController

  • Forum::Topic::PostsController

could be

ForumsController TopicsController PostsController

and then your resources can be:

map.resources :forums do |forums| forums.resources :topics do |topics|

topics.resources :posts

end end

it will still give you long urls like /forums/1/topics/1/posts/1

I’ve done some changes to get slightly shorter urls like this:

map.resources :forums, :as => ‘f’ do |forums|

forums.resources :topics, :as => ‘t’ do |topics|

topics.resources :posts, :as 'p'

end

end

which then makes ther urls /f/1/t/1/p/1 and then if you use to_param in your models, you can get /f/1-my-forum/t/1-my-topic/p/1-my-post

The to_param is done in the model

class Forum < ActiveRecord::Base

def to_param “#{id}-#{name.downcase.gsub(/[^0-9a-z]+/, ‘-’)” end end

Ok. But what if i will have following models:

Video, Audio, Video::Playlist, Audio::Playlist

and resouces:

video -> has_many -> video/playlists audio -> has_many -> audio/playlists

I would probably make a single playlist model/controller that could deal with both audio and video

Your way isn’t wrong, I was just suggesting another way

So if you have BlogPost, ForumPost, FooPost, BarPost, etc. you suggest make a single controller?

So if you have BlogPost, ForumPost, FooPost, BarPost, etc. you suggest

make a single controller?

Not a definitive ‘yes’ but in some cases it can work especially if the playlist has exactly the same functionality but is working on more than one type of model.