Sent from my iPhone

On Aug 31, 2011, at 1:21 PM, 7stud -- <[email protected]> wrote:

> Conrad Taylor wrote in post #1019461:
>> On Wed, Aug 31, 2011 at 12:01 PM, Conrad Taylor <[email protected]>
>> wrote:
>> 
>>>> I read that, and I don't see how applying a regex to the id will help.
>>> 
>>> match '/:id' => 'users#show', :constraints => { :id => /[0-9]+/ }
>>> 
>> 
>> A much better regular expression which matches a value of an :id should
>> be
>> something like the following:
>> 
>> match '/:id' => 'users#show', :constraints => { :id => /^[1-9]\d*/ }
>> 

The above can easily be fixed by adding a $ after the *.  For example,

/^[1-9]\d*$/

The x after the final / isn't needed being that the begin and end tokens take 
care of that for you.  Also, you can do all this in the context of a routes.rb 
using the constrains option to match.  Thus, one shouldn't have to define a 
method in a separate file unless there's plan to do this for other controller 
actions.  If this is the case, then this becomes a one line method that returns 
a regex.  In short, the Rails 3 routing provides much of what you're doing 
below out of the box.

Good luck,

-Conrad

> 
> There are still a couple problems with that regex:
> 
> 1)  There is no ending anchor so an id of '10A' would match.
> 2)  You can't use anchors in a regexp for a constraint anyway.
> 
> 
> This works for me:
> 
> 
> class UserShowConstraint
>  def matches?(request)
>    dirname, id = File.split(request.path)
>    return false unless dirname == '/users'
> 
>    id =~ /
>       \A     #start of string
>       [1-9]  #not 0
>       \d*    #one or more digits, including 0
>       \z     #end of string
>    /x
> 
>  end
> end
> 
> TestApp::Application.routes.draw do
> 
>  root :to => "pages#home"
> 
>  resources :users, :except => [:show]
>  match "/users/*id" => "users#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.
> 

-- 
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