views:

143

answers:

3

Hi All

I am currently using the Permalink_fu plugin which correctly creates the following URI:

http://localhost:3000/pages/some-permalink-to-page

I now want to configure my routing so that I can drop the /pages/ part from the URI leaving me with:

http://localhost:3000/some-permalink-to-page

I tried adding the following to the bottom of my config/routes.rb file:

map.connect ':permalink', :controller => 'page', :action => 'view'

but I get the following error, when I try the new URI:

uninitialized constant PageController

Do you have any suggestions? I'm running Rails 2.2.2 and am reluctant to try edge rails just yet.

Many thanks,

Ben...

A: 

In your route, should :controller be "pages" (plural)?

Mike Buckbee
A: 

Thanks Mike, I'd made a number of errors. This is how I got it working. In the routes.rb file add the following routing, near the bottom of the page:

map.connect ':id', :controller => 'pages', :action => 'show'

The problem is then now any bad URL is going to fail badly, e.g.

http://localhost:3000/this-permalink-doesnt-exist

Will lead to a failure not a 404 error.

I fixed this by adding the following line to my pages_controller.rb show action:

  def show
    @page = Page.find_by_permalink(params[:id])
    if @page.nil? then
      render :file => 'public/404.html', :status => '404'
    else
      respond_to do |format|
        format.html # show.html.erb
        format.xml  { render :xml => @page }
      end
    end
  end

Now I get the correct behaviours for all variations of the URL:

http://localhost:3000/pages/some-permalink-to-page
http://localhost:3000/some-permalink-to-page
and the if an invalid permalink is entered
http://localhost:3000/no-such-permalink
gets rendered to the default public/404.html file.

Hope that helps anyone else, and thanks again Mike. Ben...

A: 

There is a typo in your routes.rb entry:

map.connect ':permalink', :controller => 'page', :action => 'view' 

It should read:

map.connect ':permalink', :controller => 'pages', :action => 'view'

The :controller parameter is the singular name of the controller, should be 'pages'

Jose Fernandez