Help me get routing right

29 views
Skip to first unread message

Kaito Michishige

unread,
Apr 1, 2013, 11:26:25 AM4/1/13
to espresso-...@googlegroups.com
Hey,

I've been trying to get this right for a few days, but I haven't been able to. I'll explain my desired setup. Please criticize and tell me how I'd get it done. Thanks in advance for your time.

Application consists of front end and back end. Each in their own controller. Front end controller serves everything except /admin, which is served by the back end controller How do I mount the two controllers?

Inside the front end controller, / is served by the index method. Can I make a method that responds to something such as '/:category/'? When I try, it seems like the index method complains about excessive parameters.

Silviu Rusu

unread,
Apr 1, 2013, 11:35:19 AM4/1/13
to espresso-...@googlegroups.com
can i see your code?

Kaito Michishige

unread,
Apr 1, 2013, 11:53:52 AM4/1/13
to espresso-...@googlegroups.com
Hey Silviu, thanks for responding. There's really not much to be seen. My app looks very much like the gists you posted, give or take a few lines, since I'm concentrating on the html/css/js for now.

EspressoApp.new do
mount Frontend, '/'
mount Backend, '/admin'
end

Doesn't do what you think it does, it seems. I still need to `map '/'` in each controller, or else it will serve on /frontend, and /admin/backend.

As for parametrizing the index method, I don't think that's a good idea. / serves the landing page, while /:category serves a list of items in a category. I don't think these should be handled by the same method, don't you agree?

Silviu Rusu

unread,
Apr 1, 2013, 12:12:39 PM4/1/13
to espresso-...@googlegroups.com
well, yes, first example will serve /frontend and /admin/backend

it is for cases when you are using self-descriptive controller names, like States will serve /states without needing to use map.

if you need controller to serve a custom address, use map.

you can make index action to serve both landing page and categories, just make category argument optional:

def index category = nil

  pass :category if category # will stop execution and pass control to category action

  # render landing page
end

def category name
end

Silviu Rusu

unread,
Apr 1, 2013, 12:14:16 PM4/1/13
to espresso-...@googlegroups.com

also, i would recommend Enginery to get started - https://github.com/espresso/enginery

Silviu Rusu

unread,
Apr 1, 2013, 12:20:32 PM4/1/13
to espresso-...@googlegroups.com
btw, if you put controllers under some module, module name will serve as root mount point and you do not need to use map in any of controllers:

module Admin
  class Cities < E
    # actions here
  end

  class States < E
    # actions here
  end
end

EspressoApp.new do

end

# or just
EspressoApp.new(:automount).run

Silviu Rusu

unread,
Apr 1, 2013, 12:21:43 PM4/1/13
to espresso-...@googlegroups.com
err in last message:

module Admin
  class Cities < E
    # actions here
  end

  class States < E
    # actions here
  end
end

EspressoApp.new do
  mount Admin
  run
end

# or just
EspressoApp.new(:automount).run

Michishige Kaito

unread,
Apr 1, 2013, 12:23:57 PM4/1/13
to espresso-...@googlegroups.com
Thanks again.

I'm not sure I'm happy with using pass. It doesn't declare the flow
clearly. But if that's the only option, I'll have to stick to it. This
is a situation where sinatra style routes are much cleaner, while
espresso's approach is much cleaner in almost every other situation. I
just don't like polluting my URIs because of a framework's
shortcomings. There's basically no reason to use /category/foo over /foo
except easier parsing for the router.

I don't want to use a generator. I like having absolute control over the
project tree, and crafting things by hand. I'd rather type everything
out.
> --
> You received this message because you are subscribed to the Google Groups "Espresso Framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to espresso-framew...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

--
Michishige Kaito

BOFH excuse #79:

Look, buddy: Windows 3.1 IS A General Protection Fault.

Silviu Rusu

unread,
Apr 1, 2013, 12:37:16 PM4/1/13
to espresso-...@googlegroups.com

pass is not the only option, you are free to use return cause it is inside a method, not inside a block:

def index category = nil
  return category(category) if category

  # render landing page
end

if you still want to fully export it from index action, you can use a hook:

before :index do
  pass :category if action_params[:category]
end

def index category = nil
  # render landing page
end

def category name
  # render category
end


> To unsubscribe from this group and stop receiving emails from it, send an email to espresso-framework+unsub...@googlegroups.com.

Michishige Kaito

unread,
Apr 1, 2013, 1:12:51 PM4/1/13
to espresso-...@googlegroups.com
I'll probably go with return, since at least it's clear what I'm trying
to do. However, understanding the index method would require reading the
category method. That's bad design. This is why I said that in this
situation, sintatra style routing is better, because you explicitly
declare that index *only* serves /, while the other method serves
/:category.

Thanks a lot for explaining :)
> > an email to espresso-framew...@googlegroups.com <javascript:>.
> >
> > > For more options, visit https://groups.google.com/groups/opt_out.
> > >
> > >
> >
> > --
> > Michishige Kaito
> >
> > BOFH excuse #79:
> >
> > Look, buddy: Windows 3.1 IS A General Protection Fault.
> >
>
> --
> You received this message because you are subscribed to the Google Groups "Espresso Framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to espresso-framew...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

--
Michishige Kaito

BOFH excuse #225:

It's those computer people in X {city of world}. They keep stuffing things up.
Reply all
Reply to author
Forward
0 new messages