Forum: Ruby on Rails Help with Routes

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.
4034df6027cbecf3f4a19a711a248536?d=identicon&s=25 Steven (Guest)
on 2006-05-18 19:22
I love the routes feature for RoR...until now. I'm trying to write tests
to make sure my urls are correctly mapping to the routes I expect them
to map to. This is driving me nuts because I simply can't get any of my
tests to pass. I have thus concluded that routes must work differently
than I thought. I will try to explain my understanding of how routes
work. Via this process, it would be great if someone could point out
where I've gone wrong.

Here is my route:

[code]
map.download
'latform_filter/:software/:version/latform/:distribution_channel/:id',
    :controller => 'user/version',
    latform_filter => /all/,
    latform => /windows/linux/,
    :distribution_channel => /download/,
    :action => 'download',
    :id => /\d+/
[/code]

Ok, so the following url will map to this route:
http://mysite.com:3000/all/eudora/7.0.1/windows/download/5

Here's how I think the match is determined:

   1. Rails looks at the first portion of the url--"all"--and checks to
see if it will be accepted by my route. It matches my regex constraint,
so platform_filter => "all" is set.
   2. Rails inspects the next url item--"eudora"--checks to see if it
matches my mapping constraint. There is no constraint, so it matches,
sets :software => "eudora".
   3. Rails inspects the next url item--"7.0.1"--checks to see if it
matches my mapping constraint. There is no constraint, so :version =>
"7.0.1" is set.
   4. Rails inspects the next url item--"windows"--checks to see if it
matches my mapping constraint. It matches my regex constraint, so
platform => "windows" is set.
   5. Rails inspects the next url item--"download"--checks to see if it
matches my mapping constraint. It matches my regex constraint, so
:distribution_channel => "download" is set.
   6. Rails inspects the last url item--"5"--checks to see if it matches
my mapping constraint. It matches my regex constraint, so :id =>
"download" is set.
   7. All the url portions mapped, so this route will be used.
   8. Rails looks at the :controller and the :action defined in this
route and executes them, passing all the matchig url portions via the
params hash. Ex: we can access the :software portion of the url like
this--params[:software]

If any of the above steps do not match, the route is skipped, the keys
are cleared and the process is repeated with the next route.

So now I want to test my route with within the
User::VersionControllerTest class:

[code]
def test_route
url = "/all/eudora/7.0.1/windows/download/5"
opts = {:controller => 'user/version', latform_filter => "all",
:software => "eudora",
             :version => "7.0.1", latform => "windows",
             :distribution_channel => "download", :id => 5}

assert_recognizes(opts, url)     # Assertion fails!
end
[/code]

I really don't understand why this is failing, even the error message
doesn't make sense:

[code]
The recognized options <{"software"=>"eudora",
"platform"=>"windows",
"action"=>"download",
"platform_filter"=>"all",
"id"=>"5",
"controller"=>"user/version",
"version"=>"7.0.1",
"distribution_channel"=>"download"}> did not match
<{"software"=>"eudora",
"platform"=>"windows",
"platform_filter"=>"all",
"id"=>5,
"controller"=>"user/version",
"version"=>"7.0.1",
"distribution_channel"=>"download"}>
[/code]

The error message show identical options but says they don't match?

If anyone can shed some light on this, please do.

-Steven
90842970ceaf7ed19136ce7b6f8e1455?d=identicon&s=25 Michael Daines (Guest)
on 2006-05-19 00:45
(Received via mailing list)
> map.download
> 'latform_filter/:software/:version/latform/:distribution_channel/:id',
>     :controller => 'user/version',
>     latform_filter => /all/,
>     latform => /windows/linux/,
>     :distribution_channel => /download/,
>     :action => 'download',
>     :id => /\d+/

This may be a silly answer, but is that the exact code? Is it a
mistake that this says "latform_filter" rather than ":platform_filter"?

Also, I'd imagine you'd want /windows|linux/ (note the vertical bar)
for the "latform" or ":platform" requirement regexp. (Meaning
"windows" or "linux" will be matched.)


-- Michael Daines
    http://www.mdaines.com
C9c7a65848f13e2b1a226bbe43bc3672?d=identicon&s=25 Pete Yandell (pete)
on 2006-05-19 01:20
(Received via mailing list)
On 19/05/2006, at 3:22 AM, Steven wrote:
> "controller"=>"user/version",
>
> The error message show identical options but says they don't match?

They're not identical. The first one includes "action" => "download";
the second one doesn't.

Pete Yandell
http://9cays.com
This topic is locked and can not be replied to.