Forum: Ruby on Rails Namespaced and nested controllers/resouces

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
09e6bbb7f4b87c6baedb3014ff10c091?d=identicon&s=25 Dmitrij Smalko (Guest)
on 2009-01-15 14:29
(Received via mailing list)
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)
5772c599ccab3081e0fffb1d54f3b6de?d=identicon&s=25 Andrew Timberlake (andrewtimberlake)
on 2009-01-15 16:22
(Received via mailing list)
On Thu, Jan 15, 2009 at 3:28 PM, Dmitrij Smalko <dsmalko@gmail.com>
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 Timberlake
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

"I have never let my schooling interfere with my education" - Mark Twain
09e6bbb7f4b87c6baedb3014ff10c091?d=identicon&s=25 Dmitrij Smalko (Guest)
on 2009-01-15 16:48
(Received via mailing list)
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 Timberlake" <and...@andrewtimberlake.com>
5772c599ccab3081e0fffb1d54f3b6de?d=identicon&s=25 Andrew Timberlake (andrewtimberlake)
on 2009-01-15 17:13
(Received via mailing list)
2009/1/15 Dmitrij Smalko <dsmalko@gmail.com>

>
> 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 Timberlake
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

"I have never let my schooling interfere with my education" - Mark Twain
09e6bbb7f4b87c6baedb3014ff10c091?d=identicon&s=25 Dmitrij Smalko (Guest)
on 2009-01-16 10:56
(Received via mailing list)
So if you have BlogPost, ForumPost, FooPost, BarPost, etc. you suggest
make a single controller?

On 15 ÑÎ×, 18:12, "Andrew Timberlake" <and...@andrewtimberlake.com>
5772c599ccab3081e0fffb1d54f3b6de?d=identicon&s=25 Andrew Timberlake (andrewtimberlake)
on 2009-01-19 08:17
(Received via mailing list)
2009/1/16 Dmitrij Smalko <dsmalko@gmail.com>

>
> 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 Timberlakehttp://ramblingsonrails.comhttp://
> www.linkedin.com/in/andrewtimberlake
> >
> > "I have never let my schooling interfere with my education" - Mark Twain
> >
>


--
Andrew Timberlake
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

"I have never let my schooling interfere with my education" - Mark Twain
This topic is locked and can not be replied to.