I have the following problem: - I have a controller called UsersController and I need to restrict the show URL to use only numbers, How can I do that? my routes.rb has this line: resources :users, :except=>[:destroy] How I add this constraint in this line? I know that there is a command :constraints, but I dont know how to use in resources ;/ Thanks :D -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
How about: match ''/:id'' => ''users/show'' resources :users, :except=>[:destroy] -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Wed, Aug 31, 2011 at 7:04 AM, Bruno Meira <goesmeira-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have the following problem: > - I have a controller called UsersController and I need to restrict > the show URL to use only numbers, How can I do that? > my routes.rb has this line: > resources :users, :except=>[:destroy] > How I add this constraint in this line? > I know that there is a command :constraints, but I dont know how to > use in resources ;/ > Thanks :D > >Bruno, I would recommend reading section 4.2 of the Rails routing guide: http://guides.rubyonrails.org/routing.html Good luck, -Conrad> -- > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
7stud -- wrote in post #1019391:> How about: > > match ''/:id'' => ''users/show'' > resources :users, :except=>[:destroy]Sorry, that doesn''t meet your requirements because both ''/1'' and ''/show/1'' will match. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Wed, Aug 31, 2011 at 10:04 PM, Bruno Meira <goesmeira-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have the following problem: > - I have a controller called UsersController and I need to restrict > the show URL to use only numbers, How can I do that? > my routes.rb has this line: > resources :users, :except=>[:destroy] > How I add this constraint in this line? > I know that there is a command :constraints, but I dont know how to > use in resources ;/ >look at http://guides.rubyonrails.org/routing.html#segment-constraints> Thanks :D > > -- > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Wed, Aug 31, 2011 at 7:27 AM, Conrad Taylor <conradwt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Wed, Aug 31, 2011 at 7:04 AM, Bruno Meira <goesmeira-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> I have the following problem: >> - I have a controller called UsersController and I need to restrict >> the show URL to use only numbers, How can I do that? >> my routes.rb has this line: >> resources :users, :except=>[:destroy] >> How I add this constraint in this line? >> I know that there is a command :constraints, but I dont know how to >> use in resources ;/ >> Thanks :D >> >> > Bruno, I would recommend reading section 4.2 of the Rails routing guide: > > http://guides.rubyonrails.org/routing.html >http://guides.rubyonrails.org/routing.html#specifying-constraints> > Good luck, > > -Conrad > > >> -- >> >> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Conrad Taylor wrote in post #1019393:> On Wed, Aug 31, 2011 at 7:04 AM, Bruno Meira <goesmeira-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > >> > Bruno, I would recommend reading section 4.2 of the Rails routing guide: >I read that, and I don''t see how applying a regex to the id will help. How about adding :show to the :except clause and doing this: match ''/:id'' => ''users/show'' resources :users, :except=>[:destroy, :show] -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Wed, Aug 31, 2011 at 10:51 PM, 7stud -- <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Conrad Taylor wrote in post #1019393: > > On Wed, Aug 31, 2011 at 7:04 AM, Bruno Meira <goesmeira-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > > > >> > > Bruno, I would recommend reading section 4.2 of the Rails routing guide: > > > > I read that, and I don''t see how applying a regex to the id will help. > How about adding :show to the :except clause and doing this: > > match ''/:id'' => ''users/show'' >having this line will match any controller''s index action so i think that''s not fine unless you place this at the bottom of your routes file.> resources :users, :except=>[:destroy, :show] >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/ }, :via => :get the via option is important so that it will only match get requests.>--> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi All, Thanks for answer my question. I read http://guides.rubyonrails.org/routing.html#segment-constraints and I guess that the best way to resolve my problem is do like Jim said, put the route in match and add except constraint in resource. Thanks All ;D -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Jim ruther Nill wrote in post #1019405:> On Wed, Aug 31, 2011 at 10:51 PM, 7stud -- <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > >> >> match ''/:id'' => ''users/show'' >> > > having this line will match any controller''s index actionAh, 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Wed, Aug 31, 2011 at 7:51 AM, 7stud -- <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Conrad Taylor wrote in post #1019393: > > On Wed, Aug 31, 2011 at 7:04 AM, Bruno Meira <goesmeira-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > > > >> > > Bruno, I would recommend reading section 4.2 of the Rails routing guide: > > > > I read that, and I don''t see how applying a regex to the id will help. > How about adding :show to the :except clause and doing this: > > match ''/:id'' => ''users/show'' > resources :users, :except=>[:destroy, :show] > >If you apply the regex to the :id, then one can restrict the value of the :id to one or more digits. In the above, :id is simply a placeholder and can accept any value. Thus, the following should work for the original poster: match ''/:id'' => ''users#show'', :constraints => { :id => /[0-9]+/ } resources :users, :except=> [ :destroy, :show ] Good luck, -Conrad --> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Wed, Aug 31, 2011 at 12:01 PM, Conrad Taylor <conradwt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Wed, Aug 31, 2011 at 7:51 AM, 7stud -- <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > >> Conrad Taylor wrote in post #1019393: >> > On Wed, Aug 31, 2011 at 7:04 AM, Bruno Meira <goesmeira-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >> > wrote: >> > >> >> >> > Bruno, I would recommend reading section 4.2 of the Rails routing guide: >> > >> >> I read that, and I don''t see how applying a regex to the id will help. >> How about adding :show to the :except clause and doing this: >> >> match ''/:id'' => ''users/show'' >> resources :users, :except=>[:destroy, :show] >> >> > If you apply the regex to the :id, then one can restrict the value of > the :id to one or more digits. In the above, :id is simply a placeholder > and can accept any value. Thus, the following should work for the > original poster: > > 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*/ } Note: The above says that I have at least one digit >= 1 starting with the first digit. -Conrad> > resources :users, :except=> [ :destroy, :show ] > >Good luck,> > -Conrad > > -- >> >> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Conrad Taylor wrote in post #1019461:> On Wed, Aug 31, 2011 at 12:01 PM, Conrad Taylor <conradwt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > 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*/ } >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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I do like Conrad said and the code works well. Thx ;D -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
My constraint stayed like that: :constraints => { :id => / [1-9]\d*/ } -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
First, why don''t you actually give an example of the url you want to map to the show action??? Second, that regex does not meet your requirements. You may think it works well, but that just means you didn''t test it well enough. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi 7stud, I wanted a URL like that: /users/:id, where :id could only be one or more integers, actually the user id. I don''t have in mind any case that this line could''nt be resolve. Could you give me an example? Thx ;D -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Sent from my iPhone On Aug 31, 2011, at 1:21 PM, 7stud -- <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Conrad Taylor wrote in post #1019461: >> On Wed, Aug 31, 2011 at 12:01 PM, Conrad Taylor <conradwt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Wed, Aug 31, 2011 at 3:07 PM, Bruno Meira <goesmeira-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi 7stud, > I wanted a URL like that: /users/:id, where :id could only be one or > more integers, actually the user id. > I don''t have in mind any case that this line could''nt be resolve. > Could you give me an example? > Thx ;D > >If this is the case, then I would recommend just simply using the following and deal with the error case within the action: resources :users For example, def show @user = User.find( params[:id] ) # This will generate an exception if the value of the params[:id] doesn''t exist (i.e. AR::RecordNotFound). ... end def show @user = User.find_by_id( params[:id] ) # This will return nil if the value of the params[:id] doesn''t exist. ... end In both cases above, you''ll have to deal with the case where the record does not exist. For example, this would be in the form of a ''Page Not Found'' for your site or something similar. In short, I would just use resources as resources and provide a good user experience when an error does occur. However, there are cases when constrains work well but I don''t think this is a good use case for it. Good luck and have fun, -Conrad> -- > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Bruno Meira wrote in post #1019487:> Hi 7stud, > I wanted a URL like that: /users/:id, where :id could only be one or > more integers, actually the user id. > I don''t have in mind any case that this line could''nt be resolve. > Could you give me an example? > Thx ;DSure, your route+constraint matches the url: /users/SallieMae5 and therefore it does not meet your stated requirements. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Conrad Taylor wrote in post #1019488:> Sent from my iPhone > > On Aug 31, 2011, at 1:21 PM, 7stud -- <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > >>> 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*$/ >Why do you continue to claim that you can use anchors in a constraint? Do you even know what an anchor is?> The x after the final / isn''t needed being that the begin and end tokens > take care of that for you.Apparently, you don''t know what regex flags do either.> Also, you can do all this in the context of > a routes.rb using the constrains option to match.No. You cannot. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Bruno Meira wrote in post #1019487:> Hi 7stud, > I wanted a URL like that: /users/:id, where :id could only be one or > more integers, actually the user id. >What is it that you find lacking in the normal rails routing, e.g. resources :users that requires you to change it? -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Thu, Sep 1, 2011 at 1:53 AM, 7stud -- <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Conrad Taylor wrote in post #1019488: > > Sent from my iPhone > > > > On Aug 31, 2011, at 1:21 PM, 7stud -- <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > > > >>> 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*$/ > > > > Why do you continue to claim that you can use anchors in a constraint? >In regards to Rails routing, the start anchor ^ is implied as stated in section 3.8 of the routing documentation.> Do you even know what an anchor is? > >> > The x after the final / isn''t needed being that the begin and end tokens > > take care of that for you. > > Apparently, you don''t know what regex flags do either. > >You''re using the x flag because you have implemented your RegEx across several lines where the spaces are not escaped. However, one could have easily written the same thing in a single line and zero unescaped spaces. In short, this is just another way to do the same thing. -Conrad> > > Also, you can do all this in the context of > > a routes.rb using the constrains option to match. > > No. You cannot. > > -- > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Conrad Taylor wrote in post #1019592:> On Thu, Sep 1, 2011 at 1:53 AM, 7stud -- <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > >> > >> > The above can easily be fixed by adding a $ after the *. For example, >> > >> > /^[1-9]\d*$/ >> > >> >> Why do you continue to claim that you can use anchors in a constraint? >> > > In regards to Rails routing, the start anchor ^ is implied as stated in > section > 3.8 of the routing documentation. >Nevertheless, you can''t use anchors in the regex which you specify for a constraint, or you will get an error: ArgumentError Regexp anchor characters are not allowed in routing requirements: /^[1-9]\d*/ Even though I tested the regex constraint before, I am now getting different results--they show that the regex has an implied anchor at both the beginning and at the end of the regex. For instance, if this is the only route in my routes.db file: match "/users/:id" => "users#show", :constraints => {:id => /[1-9]\d*/} ...then the following urls match: http://localhost:3000/users/1 http://localhost:3000/users/10 but these urls do not match: http://localhost:3000/users/a1 http://localhost:3000/users/1a http://localhost:3000/users/aa http://localhost:3000/users/01 They produce a routing error: No route matches "/users/xx" If there was no implied ''begining of string'' anchor in the regex, then the url: http://localhost:3000/users/a1 would match. And if there was no implied ''end of string'' anchor in the regex, then the url: http://localhost:3000/users/1a would match. With the following route being the only route in my routes.rb file: resources :users ...then all the following urls match: http://localhost:3000/users/1 http://localhost:3000/users/a1 http://localhost:3000/users/1a http://localhost:3000/users/aa http://localhost:3000/users/01 Just about any characters will match in the :id position. So, Jim ruther Nill gave Bruno the right suggestion from the start. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sep 1, 7:03 am, 7stud -- <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Conrad Taylor wrote in post #1019592: > > > On Thu, Sep 1, 2011 at 1:53 AM, 7stud -- <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > > >> > The above can easily be fixed by adding a $ after the *. For example, > > >> > /^[1-9]\d*$/ > > >> Why do you continue to claim that you can use anchors in a constraint? > > > In regards to Rails routing, the start anchor ^ is implied as stated in > > section > > 3.8 of the routing documentation. > > Nevertheless, you can''t use anchors in the regex which you specify for a > constraint, or you will get an error: > > ArgumentError > Regexp anchor characters are not allowed in routing requirements: > /^[1-9]\d*/ >Yes, this is correct and it''s the current expected behavior.> Even though I tested the regex constraint before, I am now getting > different results--they show that the regex has an implied anchor at > both the beginning and at the end of the regex. For instance, if this > is the only route in my routes.db file: > > match "/users/:id" => "users#show", > :constraints => {:id => /[1-9]\d*/} > > ...then the following urls match: > > http://localhost:3000/users/1http://localhost:3000/users/10 > > but these urls do not match: > > http://localhost:3000/users/a1http://localhost:3000/users/1ahttp://localhost:3000/users/aahttp://localhost:3000/users/01 > > They produce a routing error: > > No route matches "/users/xx" > > If there was no implied ''begining of string'' anchor in the regex, then > the url: > > http://localhost:3000/users/a1 > > would match. And if there was no implied ''end of string'' anchor in the > regex, then the url: > > http://localhost:3000/users/1a > > would match. > > With the following route being the only route in my routes.rb file: > > resources :users > > ...then all the following urls match: > > http://localhost:3000/users/1http://localhost:3000/users/a1http://localhost:3000/users/1ahttp://localhost:3000/users/aahttp://localhost:3000/users/01 > > Just about any characters will match in the :id position. So, Jim > ruther Nill gave Bruno the right suggestion from the start.Yes, this is exactly what I instructed Bruno to do in my initial response to his question. -Conrad> > -- > Posted viahttp://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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
hmmmm, That''s what I do to fix my routing. I guess that my question generated a little confusion ;/. Sorry for any incovenience Thx all -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hello, I wonder how to tell my routes.rb to route requests to a resource to it''s parent controller class Foo < Bar end resources :foos ===> controller :bars -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Check out the guide for routing, especially the part on "specifying a controller to use" under "customizing restful resources" http://guides.rubyonrails.org/routing.html#customizing-resourceful-routes -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/EISJQTurhMgJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi! On Mon, Sep 19, 2011 at 10:57 AM, slava <mikerin.slava-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello, > I wonder how to tell my routes.rb to route requests to a resource to > it''s parent controller > >I don''t know if what I will say is the "Rails Way", but maybe you can try: 1. Define in your config/routes.rb resources :foos 2. Create a Controller that Inherits from Bar class FoosController < BarsController 3. Reuse the methods you need calling super(): class FoosController < BarsController def create super() end end Best Regards, Everaldo> class Foo < Bar > end > > > resources :foos ===> controller :bars > > -- > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Slava Mikerin wrote in post #1022739:> Hello, > I wonder how to tell my routes.rb to route requests to a resource to > it''s parent controller > > class Foo < Bar > end > > > resources :foos ===> controller :barsresources :foos, :controller => :bars -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.