I want my application URL''s to have the name of products in the URL as opposed to the id, like so: /product/windows+xp instead of /product/view/1 /product/windows+xp/edit instead of /product/edit/1 I assume the best way then would be in the controller to use: def view @product = Product.find_by_name(params[:name]) end instead of def view @product = Product.find(params[:id]) end So my question is: 1) Would find_by_name degrade performance significantly because I''m searching by name now and not by row? If so, 2) is there a better way to handle the find in a more efficient way and still use the string name in the URL''s? Happy holidays, Chad --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
That seems like a reasonable approach to me, assuming you configure the routes accordingly. Performance will be slower due to the nature of the search, but not by much. I would suggest dropping an index onto the name field. On Dec 18, 10:16 am, "Chad Arimura" <carim...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I want my application URL''s to have the name of products in the URL as > opposed to the id, like so: > > /product/windows+xp instead of /product/view/1 > /product/windows+xp/edit instead of /product/edit/1 > > I assume the best way then would be in the controller to use: > > def view > @product = Product.find_by_name(params[:name]) > end > > instead of > > def view > @product = Product.find(params[:id]) > end > > So my question is: > > 1) Would find_by_name degrade performance significantly because I''m > searching by name now and not by row? > > If so, 2) is there a better way to handle the find in a more efficient way > and still use the string name in the URL''s? > > Happy holidays, > Chad--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
askegg wrote:> That seems like a reasonable approach to me, assuming you configure the > routes accordingly. > Performance will be slower due to the nature of the search, but not by > much. I would suggest dropping an index onto the name field.I''ve forgotten where I saw the post, but it recommended taking the approach of including the id and the name (which can change). In fact the name can be arbitrary and can be ignored. ie, allow /product/1-windows+xp/edit /product/2-windows+98 etc. Then you just ignore the non-id part, and have no performance problems, and the urls will still work if you rename the product. -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
A nice way to do this is to use the acts_as_sluggable plugin: http://www.agilewebdevelopment.com/plugins/acts_as_sluggable http://dev.2750flesk.com/acts_as_sluggable/ You can then use it in your model like this: class Article < ActiveRecord::Base acts_as_sluggable :with => ''title'' end A link in your view would be: link_to ''Read article'', :action => ''show'', :id => @article And the URL it generates would look like: /articles/show/76-omg-my-cat-is-so-cute-lol I''m using acts_as_sluggable in a couple different Rails applications and it''s working very nicely for me, a live example is: http://www.lovemygarden.net/resources/article/1-testing-and-amending-garden-soil yours, sness. http://www.sness.net On 12/17/06, Charles Lowe <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > askegg wrote: > > That seems like a reasonable approach to me, assuming you configure the > > routes accordingly. > > Performance will be slower due to the nature of the search, but not by > > much. I would suggest dropping an index onto the name field. > > I''ve forgotten where I saw the post, but it recommended taking the > approach of including the id and the name (which can change). In fact > the name can be arbitrary and can be ignored. ie, allow > > /product/1-windows+xp/edit > /product/2-windows+98 > > etc. > > Then you just ignore the non-id part, and have no performance problems, > and the urls will still work if you rename the product. > > > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
sness wrote:> And the URL it generates would look like: > > /articles/show/76-omg-my-cat-is-so-cute-lolIs there a way to add a route that drops the "show" from the URL? I''m using this route to drop "show" from the URL, but it only works if the :id part of the URL is an integer: map.connect '':controller/:id'', :action => ''show'', :requirements => { :id => /\d+/ } Any help is appreciated. -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 12/17/06, Chad Arimura <carimura-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > I want my application URL''s to have the name of products in the URL as > opposed to the id, like so: > > /product/windows+xp instead of /product/view/1 > /product/windows+xp/edit instead of /product/edit/1 > > I assume the best way then would be in the controller to use: > > def view > @product = Product.find_by_name(params[:name]) > end > > instead of > > def view > @product = Product.find(params[:id]) > endThis link does a good job of explaining, and linking to other resources on the subject. http://www.tonyspencer.com/2007/02/04/better-search-engine-friendly-urls-with-ruby-on-rails/ So my question is:> > 1) Would find_by_name degrade performance significantly because I''m > searching by name now and not by row? > > If so, 2) is there a better way to handle the find in a more efficient way > and still use the string name in the URL''s? > > Happy holidays, > Chad > > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 12/17/06, *Chad Arimura* <carimura-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > If so, 2) is there a better way to handle the find in a more > efficient way > and still use the string name in the URL''s?Take a look at Tressle http://trestle.rubyforge.org/ and http://www.rubyinside.com/trestles-replacing-rails-scaffolding-with-something-better-138.html /people Lists existing person records /people/new GET Shows an empty person form POST Creates a new person record from request parameters /people/99 Shows the person record having ID 99 /people/99/edit GET Shows a person form for the record having ID 99 POST Updates the person record having ID 99 using request parameters /people/99/destroy GET Redirects to /people/99/edit with a notice that the user must click the forms Destroy button in order to destroy a record POST Deletes the person record having ID 99 after prompting the user for confirmation Personally I hate the verb-object form and I hate the :id as an absolute. We see in wikis and blogs the use of a name parameter /people/jack There are many settings where you don''t want the user/hacker to step through the numbers. I''m a user of TWiki as a wiki and I find the "http://twiki.org/cgi-bin/view/web/topic" annoying. The "cgi-bin/view" is not needed, and as Nils Jonsson''s article points out, the move from view to edit is easier if the verb is a suffix. One day I''ll figure out the mapping for /web/topic /web/topic/edit /web.topic /web.topic/edit /web.topic?edit /web.topic?edit&skin=cosmos /web/topic?raw=on and so forth -- We know nothing about motivation. All we can do is write books about it. --Peter F. Drucker --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---