Namespaced and nested controllers/resouces


#1

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)


#2

On Thu, Jan 15, 2009 at 3:28 PM, Dmitrij S. removed_email_address@domain.invalid
wrote:

map.resources :forums do |forums|

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


Andrew T.
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

“I have never let my schooling interfere with my education” - Mark Twain


#3

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

On 15 ÑÎ×, 17:22, “Andrew T.” removed_email_address@domain.invalid


#4

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

On 15 ÑÎ×, 18:12, “Andrew T.” removed_email_address@domain.invalid


#5

2009/1/15 Dmitrij S. removed_email_address@domain.invalid

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


Andrew T.
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

“I have never let my schooling interfere with my education” - Mark Twain


#6

2009/1/16 Dmitrij S. removed_email_address@domain.invalid

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.


Andrew T.http://ramblingsonrails.comhttp://
www.linkedin.com/in/andrewtimberlake

“I have never let my schooling interfere with my education” - Mark Twain


Andrew T.
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

“I have never let my schooling interfere with my education” - Mark Twain