Topic: Better URL's/Less Coding

Hi Guys,

There has got to be a better way to create pages than the method I am currently using.  Currently I use the existing code to render each page.

# pages_controller.rb

def faq
  @pages = Page.find_faq_page

# page.rb

def self.find_faq_page
  @page = Page.find(:first, :conditions => ["name = ? and active = ?", "faq", 1 ])

# faq.rhtml

<%= @pages.body %>

<%= render :partial => 'navigation' %>

The name is always a unique name in the db (basically a permalink).  Can I define something in my routes.rb file so that the DB lookup is done based on the /:name section?  I just don't want to create every single piece as it means I have to create a few lines of code if I ever decide to add another page into the database.

I basically want to create pretty URL's with as little code as possible and without repeating myself for every page.

Cheers Guys.

Re: Better URL's/Less Coding

map.connect 'wiki/:name', :controller => "wiki", :action => "show"

def show
  @page = Page.find_by_name(params[:name], :conditions => "active = 1")

<h1> <%= %></h2>

<div> <%= page.body %> </div>

Last edited by Duplex (2007-07-22 09:13:03)

Re: Better URL's/Less Coding

Hi Duplex,

Thanks again for your wonderful help.

When I implement this code in routes:

map.connect '/:name', :controller => "pages", :action => "show"

It works fine when I manually enter the URL I want to go to.  However, when I use this code to create a link:
map.connect '/:name', :controller => "pages", :action => "show"

it tries to redirect to /pages/faq, which then causes an error as it looks for faq.rhtml.

Have I created the link incorrectly?  I seem to be referencing the controller in the link somehow.


Re: Better URL's/Less Coding

Use a named route:

#routes.rb '/:name', :controller => "pages", :action => "show"

link_to "some Page", page_path(:name =>

Re: Better URL's/Less Coding

Hi Duplex,

I don;t quite get how I use this?

This is still causing an error:
<%= link_to "FAQ", page_path(:name => %>

this is what it shows now (instead of rendering the page)

 NoMethodError in Pages#index

Showing app/views/pages/_navigation.rhtml where line #2 raised:

You have a nil object when you didn't expect it!
The error occurred while evaluating

Extracted source (around line #2):

1: <ul>
2:   <li><%= link_to "Home", page_path(:name => %></li>
3:   <li><%= link_to "FAQ", page_path(:name => %></li>
4: </ul>

Re: Better URL's/Less Coding

@page seems to be nil ... if wou want to link to a page, you of course need a @page object to pass the page's name to the link helper ...

ah ... just see you used "@pages" for this. Kind of confusing as it is just a single page wink

page_path(:name =>

Last edited by Duplex (2007-07-22 09:40:58)

Re: Better URL's/Less Coding

OK, so that fixed that problem.

Now I am only able to create a link to the current page.

<%= link_to "FAQ", page_path(:name => %>

That is creating a link to /home instead of /pages.

Re: Better URL's/Less Coding

Used a named route. so instead of map.connect use map.page_name or something. Then create the route by typing page_name_path( and pass the name as a variable.

Rails, Javascript, Actionscript and More.

Re: Better URL's/Less Coding

Sorry I still don't quite get what you mean?  Do you mean I need to create a route.rb entry for every page?  Because that will go against the ability to create a new page in the DB without editing the code.


Re: Better URL's/Less Coding

The named route we created is only for URLs like:

etc. They are routed to the show action of the pages controller.

now if you want to make a link to the "faq" page:

<%= link_to "FAQ", page_path(:name => "faq") %>

It's quite straight-forward i think wink

Last edited by Duplex (2007-07-22 10:55:45)