show a database record by word instead of an id number, ruby on rails guide

These instructions are to show you how to use a word in the url instead of an ID.

Like having a url that looks like this:

http://www.comickeith.com/biographies/keith

instead of

http://www.comickeith.com/biographies/1

This is much better for Search Engine Optimization, friendly urls for the public, easy to remember, and just looks better.

These instructions assume that you don’t have a “shortname” field in your table yet.

1 in your command line interface, inside the project directory, run this:

script/generate migration add_shortname

2 open the file db/migrate/LATESTTIMSTAMP_add_shortname.rb

This is the code that should be inside the class:

def self.up

add_column “pages”, “shortname”, :string

end

def self.down

remove_column “pages”, “shortname”

end

The “up” is run when you migrate, the “down” is run if you rollback

2b Run this at your command line:

rake db:migrate

3 open app/views/admin/Pages/_form.html.erb

Add this into the form:

<tr><td><p><label>Short Name:&nbsp;</label></td><td><%= f.text_field :shortname %></p></td></tr>

3 open the Page model in app/models/page.rb

Add these two lines:

validates_presence_of :shortname
validates_uniqueness_of :shortname

3b now go into your website into the admin section and put shortnames in all your records, you have to or you’ll get errors if you go on.

If you don’t have your web forms done for your adminstrative area, better go do that now, because you won’t be able to finish this target. or you can put data in the database using another way. but I suggest getting the admin forms done now. it will save you time in the long run.
See Create administrative area in ruby on rails

4 open app/controllers/pages_controller.rb, and add the following code:

#this line goes outside the defs, at the top of the class
rescue_from ActiveRecord::RecordNotFound, :with => :rec_not_found

#add the following code:
def show

if params[:id].nil?

@page = Page.find(:first)

else

@page = Page.find_by_shortname(params[:id])
raise ActiveRecord::RecordNotFound unless @page

end

end

def rec_not_found

@page = Page.find(:first)
render :action=>’show’

end

5 open app/views/layouts/application.erb and change the following line of code in the loop:

<%=link_to page.title, “/pages/”+ page.shortname  , :class => “smlink” %>

In order for the above line of code to work you have to have some other function put in which is in my other blog posts.

If you don’t have that function in, you can just hard code a link so see that it works or type it in the browser address, like this:

<a href=”/pages/profile”>Profile</a>

or see create a list of database items in admin area
for how to create a basic loop that you can put in application.erb

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.