Help on enabling subdomain as the controller name (default)?

1 view
Skip to first unread message

satynos

unread,
Dec 19, 2008, 10:42:28 AM12/19/08
to resources_controller
I am trying to have blogs application reside at blogs.example.com and
the default (root) action be index action of BlogsController.

I achieved this in the routes with the following:

map.blogs ' ', :controller => 'blogs', :index => 'index'

and the blogs.example.com/id (id is blog id) with

map.blog ':id', :controller => 'blogs', :index => 'show'

The routing seems to work fine, but the pages are displaying the
following errors:

error when I open blogs.example.com/

Could not recognize segment 'blogs' in route:
GET /
{:controller=>"blogs", :action=>"index"}

Check that config/routes.rb defines a route named 'blogs'
for controller: BlogsController"

error when I open blogs.example.com/1

Could not recognize segment 'blogs' in route:
GET /:id/
{:controller=>"blogs", :action=>"index"}

Check that config/routes.rb defines a route named 'blogs'
for controller: BlogsController"

It is obvious that the resources_controller is looking for blogs
segment the url (being in BlogsController) and throwing the above
mentioned errors. Is it possible for resources_controller to recognize
the routes in the absence of blogs segment in the url.

I would like to have this functionality so that I can reside all my
applications (blogs, forums, groups,...) reside on their own
subdomains (but all the application share the common database). This
functionality help me realease each application as a individual
application in OpenSocial.

Thanks in advance. Any feedback is greatly appreciated.


Jason Lee

unread,
Dec 19, 2008, 3:10:05 PM12/19/08
to resources_controller
Hi Satynos,

On Dec 19, 3:42 pm, satynos <dharamgollap...@gmail.com> wrote:
> error when I open blogs.example.com/
>
> Could not recognize segment 'blogs' in route:
>   GET    /
> {:controller=>"blogs", :action=>"index"}
>
> Check that config/routes.rb defines a route named 'blogs'
>   for controller: BlogsController"

The solution for this is use the :resource_path option in config/
routes.rb like this,

map.root :controller => 'blogs', :action => 'index', :resource_path
=> '/blogs'

as per 'example 6' at http://plugins.ardes.com/doc/resources_controller/classes/Ardes/ResourcesController.html

To use the subdomain to map directly to a blog instance you can do
this,

map.root :controller => 'blogs', :action => 'show', :resource_path
=> '/blog'
map.resource :blog

class ApplicationController < ActionController::Base
map_resource :blog, :singleton => true, :class => Blog, :find
=> :get_blog
private
def get_blog
Blog.find_by_hostname(request.host) || raise
(ActiveRecord::RecordNotFound, request.host + " blog not found")
end
end

class BlogsController < ApplicationController
resources_controller_for :blog, :singleton => true, :class =>
Blog, :find => :get_blog
end


I don't how you'd go about mapping a subdomain to a resource type
using resources_controller.

Jason.

satynos

unread,
Dec 19, 2008, 3:51:23 PM12/19/08
to resources_controller
Jason:

Thanks for the feedback and I got it working. At this moment the only
way it is possible is by adding my custom routes (following).

map.resources :blogs do |blog|
blog.resources :articles do |article|
article.resources :comments
end
end

Without the following routes, resources_controller has tough time
resolving the resources (due to the absence of the parent resource)

map.test '', :controller => 'blogs', :action =>
'index', :resource_path => '/blogs'
map.test1 'new', :controller => 'blogs', :action =>
'new', :resource_path => '/blogs/new'
map.test2 ':id', :controller => 'blogs', :action =>
'show', :resource_path => '/blogs/:id'
map.test3 ':id/edit', :controller => 'blogs', :action =>
'edit', :resource_path => '/blogs/:id/edit'
map.test4 ':blog_id/articles', :controller => 'articles', :action =>
'index', :resource_path => '/blogs/:blog_id/articles'
map.test5 ':blog_id/articles/new', :controller =>
'articles', :action => 'new', :resource_path => '/blogs/:blog_id/
articles/new'
map.test6 ':blog_id/articles/:article_id', :controller =>
'articles', :action => 'show', :resource_path => '/blogs/:blog_id/
articles/:article_id'
map.test7 ':blog_id/articles/:article_id/comments', :controller =>
'comments', :action => 'index', :resource_path => '/blogs/:blog_id/
articles/:article_id/comments'

Can you think of any other easier way than this?

The reason I want to have it this way if possible is that I will have
subdomains called blogs, groups, ... each one as a separate
application with shared database. And when the subdomain is blogs I
don't want to add another 'blogs' (which I fee redundant). Ultimately
I want to have the following for the

blogs.example.com (Blogs Application)
blogs.example.com/blog_name
blogs.example.com/blog_name/articles
blogs.example.com/blog_name/articles/article_name/comments

groups.example.con(Groups Application)
groups.example.com/group_name
groups.example.com/group_name/forums
groups.example.com/group_name/galleries
groups.example.com/group_name/videos

Another reason for serving each application at a separate domain as a
Opensocial app is that each application will have ONLY the routes and
the code (controllers, models, plugins, libs,...) that it absolutely
needs and I feel this should help in performance.

Any feedback is appreciated.

-Satynos

Ian White

unread,
Dec 19, 2008, 4:33:53 PM12/19/08
to resources_...@googlegroups.com
Hi Satynos,

I've not tried to do what you're attempting, so I'd be interested in
seeing how you get it working.

RC does rely on the request path to recognize resources, but even if
you weren't using RC it seems to me that you'd still have to specify a
whole bunch of routes to get the effect that you want.

I.e. I think your main problem is going to be that Rails does not give
any nice helper in routing for constructing routes in
this way. But if you take a look at the code in AC::Routing
(especially the 'resource' and 'resources' method) you might be able
to create your own 'headless_resources' method.

If you were attempting to use a particular object as a subdomain - ie.

site1.domain.org/blog => Site.find_by_name('site1').blog
site2.domain.org/blog => Site.find_by_name('site2').blog

then you would use a filter (before anything else) to find the site by
parsing the request URI (and make it accessible via #site), then you
could set up your top level resources to use that

class BlogController < ApplicationController
resources_controller_for :blog, :singleton => true do
site.blog
end
end

# and for any routes that reside under blog (in ApplicationCOntroller
or somewhere shared)
map_enclosing_resource :blog, :singleton => true do
site.blog
end

Cheers,
Ian

satynos

unread,
Dec 21, 2008, 8:15:41 PM12/21/08
to resources_controller
Ian:

Sorry for the delay in my response. Its nice to see a response from
the author himself.

> RC does rely on the request path to recognize resources, but even if  
> you weren't using RC it seems to me that you'd still have to specify a  
> whole bunch of routes to get the effect that you want.

I realized this after looking at resources_controller code more
closely, especially the recognize_route method and its freinds.
Nevertheless, I am happy to realize that atleast I can get it to work
by adding some custom routes along with the resource/resources based
routes (shown in my previous response). Once I am done done with my
app, I will certainly post my code, but it might take a while.

BTW: I did make some changes (mostly refactoring) in the
resources_controller which will allow it to be reloaded when I use
config.reload_plugins to true (part of Rails 2.2.2 changes). I needed
this functionality (reloading) because I was playing with the plugin
with several changes and I want those changes to be reflected
immediatley instead of restarting the server every time. If you are
interested I can send you the code to your email as an attachment.
(please provide your email id if interested). Some of the changes that
I am playing with include automatic resource type recoginition from
the Controller name instead of manually specifying it with "for",
enhansing the block options like in make_resourceful for callbacks and
others.

In your blog, I saw somewhere that someone has provided a pastie about
automatic resource type recognition from the controller name. Is there
any particular reason for you to not applying that to the plugin?

Also is it not possible to automatically recognizing if the resource
is singleton or not by using something like

if enclosing_resource.respond_to?(:images)
enclosing_resource.send(:images).find
elseif enclosing_resource.respond_to?(:image)
enclosing_resource.send(:image)
end

which will also eliminate the need for the developer to specify if the
resource is singleton or not.

Thanks in advance.

-Satynos
> >> as per 'example 6' athttp://plugins.ardes.com/doc/resources_controller/classes/Ardes/Resou...

Ian White

unread,
Dec 21, 2008, 8:29:16 PM12/21/08
to resources_...@googlegroups.com
Hi Satynos,

It's great that you're willing to improve RC, I'd love to see your
changes/ideas.

It would be best if you could fork the repo on github, and put all of
the changes in different branches, then I can review them all
separately and merge them in if appropriate. If you can't do this
then reply to my email directly (It's in the headers of this message).

Cheers,
Ian

satynos

unread,
Dec 21, 2008, 8:53:11 PM12/21/08
to resources_controller
Ian:

Though I know how to fork the project at git, my knowledge of using
git ends there, I am not quite sure how to merge the changes that I
make on my computer to the git, I guess I will be able to find them at
the git faq, I will look into that and will submit my changes and send
you an email at the earliest of my convenience.

When you get a chance, could you provide me your feedback on the last
two questions I aksed in my previous response?

-Satynos

Ian White

unread,
Dec 21, 2008, 9:00:27 PM12/21/08
to resources_...@googlegroups.com
Hi Satynos,

> In your blog, I saw somewhere that someone has provided a pastie about
> automatic resource type recognition from the controller name. Is there
> any particular reason for you to not applying that to the plugin?

At the time I though it was just a little too magical, but so many
people have requested it that I shall stick it in soon.

I haven't figured out yet whether to use the controller name, or the
route to introspect the resource

The syntax suggested by ChrisCruft, that I like is

resources_controller :for => :posts

where :for is optional

> Also is it not possible to automatically recognizing if the resource
> is singleton or not by using something like

It's a really nice idea, It might be a bit trickier than what you
suggest, because we don;t know if the resource is singular or plural

ie (this just illustrates the problem nicely)

resources_controller :for => :fish
Model.has_many :fish

resource_controller :for => :fish
Model.has_one :fish

In both of these cases, the model responds to :fish, but in one case
it's singleton, the other it's collection.

There might be another way to introspect this stuff though, I would
welcome you to figure it out. And I would certainly welcome some
spec'd patches of pull requests ;)

Cheers,
Ian

Ian White

unread,
Dec 21, 2008, 9:01:50 PM12/21/08
to resources_...@googlegroups.com
HI Satynos,

> the git faq
Once you get into git, you'll wonder how you did without it!

> When you get a chance, could you provide me your feedback on the last
> two questions I aksed in my previous response?

Was just in the middle of doing so :)

Cheers,
Ian

Reply all
Reply to author
Forward
0 new messages