Jim ruther Nill wrote in post #1019405:
> On Wed, Aug 31, 2011 at 10:51 PM, 7stud -- <[email protected]> wrote:
>
>>
>> match '/:id' => 'users/show'
>>
>
> having this line will match any controller's index action


Ah, I see.  :id will match anything, e.g.

/dog
/frog
/users
/pages
/hello_world
/1
/200



> in the Segments Constraints section of the rails guide, you'll see the
> first
> line of code.
> following that code, you can use
>
> match 'users/:id' => 'users#show', :constraints => { :id => /\d/ }, :

..and the constraint there says to only match a number in the :id
position of the url.   But is that route any different than the show
route created by resources()?   It was my understanding that the op
wanted a url consisting of only a number, so I think the route should be
more like:

match '/:id' => users#show, :constraints => {:id => /\d+/}, :via =>
'get'


> via => :get
>
> the via option is important so that it will only match get requests.
>

I wonder why people post ambiguous questions and expect people who read 
them to know what they are thinking--instead of giving a simple example 
that leaves nothing in doubt.


However, that will still match routes like:

/a2b


It looks like you can write a custom matcher pretty easily, like this:


class UserShowConstraint
  def matches?(request)

    request.path =~ /
       \A  #start of string
       \/  #forward slash
       \d+ #one or more digits
       \z  #end of string
    /x

  end
end


And then use the route:

match "*id" => "user#show",  :constraints => UserShowConstraint.new,
:via => "get"

-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to