Resource Routing to root of domain

1 view
Skip to first unread message

Kennard Smith

unread,
Mar 25, 2009, 10:39:45 PM3/25/09
to merb
I'm trying to change the path of a resource in my app to /

For example, if I had

resources :books

everything is dandy with normal URLs

/books/1
/books/new

but, I want my resources to be the root of the domain like

/1
/new

I can't find a way to do it.

i've tried a few things

resources :books, :path => "/"

and

resources :books, :path => ""

do not work, as the url generation breaks.

Any ideas?

Michael D'Auria

unread,
Mar 26, 2009, 9:22:22 AM3/26/09
to me...@googlegroups.com
This is merely a guess, but check out the default route handler, perhaps you can default a particular resource?

Michael

Kennard Smith

unread,
Mar 27, 2009, 2:52:46 PM3/27/09
to merb
Thanks for the reply

unfortunately the default routes don't work with resources

On Mar 26, 6:22 am, "Michael D'Auria" <michael.dau...@gmail.com>
wrote:
> This is merely a guess, but check out the default route handler, perhaps you
> can default a particular resource?
> Michael
>

Stephen Eley

unread,
Mar 27, 2009, 3:41:40 PM3/27/09
to me...@googlegroups.com
On Fri, Mar 27, 2009 at 2:52 PM, Kennard Smith <stand...@gmail.com> wrote:
>
> Thanks for the reply
> unfortunately the default routes don't work with resources

You're right, you can't do it with the nice little resource helpers.
But you can do it yourself with matching. I had this same issue with
a CMS proto-slice I was working on. Here's how I solved it, pretty
much:

Merb::Router.prepare do
identify(Content => :id) do
match("/(:id)").to(:controller => "contents") do
match("(/)contents").to(:action => "index").name(:contents)
match("/edit").to(:action => "edit").name(:edit_content)
match("(/)new").to(:action => "new").name(:new_content)
match("/delete").to(:action => "delete").name(:delete_content)
match(:method => :put).to(:action => "update").name(:update_content)
match(:method => :delete).to(:action => "destroy").name(:destroy_content)
match(:method => :post).to(:action => "create").name(:create_content)
match(:method => :get).to(:action => "show").name(:content)
end
end
end

That "contents" special URL is due to my functional requirement that
the / root would show a homepage (with an ID being set implicitly in
the controller code), not the index of all pages. That's also why the
(:id) is optional. If you wanted / to be the index you could
rearrange some of this. Oh, and the need for the optional (/) in
front of 'new' but not the other methods appears to be some sort of
edge case quirk. It took me way too long to figure out how to make
that work.

Point is, it's still a RESTful resource. I had to write the paths
myself, but it all works the way Merb resources are supposed to.


--
Have Fun,
Steve Eley (sfe...@gmail.com)
ESCAPE POD - The Science Fiction Podcast Magazine
http://www.escapepod.org

Kennard Smith

unread,
Mar 29, 2009, 11:42:59 PM3/29/09
to merb
Thanks a bunch for the advise and code, works like a charm.


On Mar 27, 12:41 pm, Stephen Eley <sfe...@gmail.com> wrote:
> On Fri, Mar 27, 2009 at 2:52 PM, Kennard Smith <standso...@gmail.com> wrote:
>
> > Thanks for the reply
> > unfortunately the default routes don't work with resources
>
> You're right, you can't do it with the nice little resource helpers.
> But you can do it yourself with matching.  I had this same issue with
> a CMS proto-slice I was working on.  Here's how I solved it, pretty
> much:
>
> Merb::Router.prepare do
>   identify(Content => :id) do
>     match("/(:id)").to(:controller => "contents") do
>       match("(/)contents").to(:action => "index").name(:contents)
>       match("/edit").to(:action => "edit").name(:edit_content)
>       match("(/)new").to(:action => "new").name(:new_content)
>       match("/delete").to(:action => "delete").name(:delete_content)
>       match(:method => :put).to(:action => "update").name(:update_content)
>       match(:method => :delete).to(:action => "destroy").name(:destroy_content)
>       match(:method => :post).to(:action => "create").name(:create_content)
>       match(:method => :get).to(:action => "show").name(:content)
>     end
>   end
> end
>
> That "contents" special URL is due to my functional requirement that
> the /rootwould show a homepage (with an ID being set implicitly in
Reply all
Reply to author
Forward
0 new messages