hao_maike
hao_maike

Reputation: 3039

Ruby on Rails match routing error

I'm trying to make a Rails application that serves simple static HTML pages. I followed Mikel's tutorial here (it involves making a Pages controller and setting up some routing) but I keep getting an error message.

I made a app/views/site/pages/_about.html.erb file to contain my About page. After starting the rails server, I try to go to http://localhost:3000/about/ but it gives me a Routing Error because I have an "uninitialized constant Site."

My project is uploaded to GitHub if you want to take a look at the code.

Edit: here's my config/routes.rb file:

NINAgallery::Application.routes.draw do
  match ':page_name' => 'site/pages#show'
end

And here's the important part of my app/controllers/pages_controller.rb file:

class PagesController < ApplicationController

  layout 'site'

  def show
    @page_name = params[:page_name].to_s.gsub(/\W/,'')
    unless partial_exists?(@page_name)
      render 'missing', :status => 404
    end
  end

  # extra code for handling 404 errors goes here

end

Upvotes: 0

Views: 300

Answers (4)

e3matheus
e3matheus

Reputation: 2132

Besides the namespace problem, you also needed to add the 'app' Gem to the Gemfile, as explained in the tutorial.

I don't know why you removed the caching of the static pages in your working code. I made a pull request with the app working and maintaining the cache problem. If another person is interested, the code is here

Also ryan bates has a tutorial called "Semi static pages" that does something similar. I would encourage you to follow his solutions because there are very rarely mistaken.

Upvotes: 0

Damien
Damien

Reputation: 27483

Your application is called NINAgallery.

Replace Site in pages_controller.rb line 27 by NINAgallery.

PS:

I just took a peek at the so-called tutorial. You are taking really really really bad habits.

Some resources to take very good basics:

If you like tutorials: http://ruby.railstutorial.org/

And there are plenty of books about rails. All good.

Upvotes: 1

Marek Př&#237;hoda
Marek Př&#237;hoda

Reputation: 11198

The last line in the PagesController is this:

ValidPartials = Site::PagesController.find_partials

That means that the PagesController is contained in a Site module. But there is no Site module in your app.

I think simple removing Site:: should fix the problem:

ValidPartials = PagesController.find_partials

Plus the route:

match ':page_name' => 'pages#show'

Upvotes: 1

Frederick Cheung
Frederick Cheung

Reputation: 84132

site/pages#show means the show action in Site::PagesController

You either need to put your controller in the namespace your routes imply or change the route

Upvotes: 1

Related Questions