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:
- 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
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.
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.
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
sness wrote:
And the URL it generates would look like:
/articles/show/76-omg-my-cat-is-so-cute-lol
Is 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.
On 12/17/06, Chad A. [email protected] wrote:
def view
@product = Product.find_by_name(params[:name])
end
instead of
def view
@product = Product.find(params[:id])
end
This 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:
On 12/17/06, Chad A. <[email protected]
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 form’s
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