On Sep 25, 2008, at 1:23 PM, Trek wrote:

I'm using Routes 1.10 and seeing some odd behavior when I
use .resources where a valid url (either entered by hand or generated
by url_for) doesn't match any resources.

map.resource sets up all routes to require specific HTTP methods. This means the match won't work unless you have a fake environ indicating whether its a HTTP GET, or POST, etc. For example, this is how the resource tests are run in Routes:

        m = Mapper()
        m.resource('person', 'people')
        m.create_regs(['people'])

        con = request_config()
        con.mapper = m
        def test_path(path, method):
env = dict(HTTP_HOST='example.com', PATH_INFO=path, REQUEST_METHOD=method)
            con.mapper_dict = {}
            con.environ = env

        test_path('/people', 'GET')
        eq_({'controller':'people', 'action':'index'}, con.mapper_dict)
        test_path('/people.xml', 'GET')
eq_({'controller':'people', 'action':'index', 'format':'xml'}, con.mapper_dict)


Setting con.environ internally calls mapper.match, after setting up the environ. Alternatively, this will work:

from routes import *
from routes.util import url_for

m = Mapper()
m.resource('blog', 'blogs')

m.create_regs(['blogs'])
m.environ = {'REQUEST_METHOD':'GET'}

So interactively:
>>> m.match('/blogs')
{'action': u'index', 'controller': u'blogs'}
>>> m.match('/blogs/2')
{'action': u'show', 'controller': u'blogs', 'id': u'2'}

In case you're worried about setting m.environ, note that its a thread- local, so setting it from different threads before matching won't be a problem.

Cheers,
Ben

Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to