views:

3224

answers:

4

Looking at different options:

One is to just put the static pages in the public/ folder, but I do want the header from layout/application to be consistent.

I tried this, but I got an error:

in routes.rb:
map.connect '*path', :controller => 'content', :action => 'show'

in content_controller.rb:
def show
  render :action => params[:path].join('/')
end

All I want is an easy way to put together things like my faq, contact, tos, privacy, and other non-application type pages somewhere easy by just creating an .rhtml. who has done this?

+5  A: 

Rendering an action doesn't make sense. You'll want to render a template (or a file) with a layout.

# Path relative to app/views with controller's layout
render :template => params[:path]

# ... OR

# Absolute path. You need to be explicit about rendering with a layout
render :file => params[:path], :layout => true

You could serve a variety of different templates from a single action with page caching.

# app/controllers/static_controller.rb
class StaticController < ApplicationController
  layout 'static'

  caches_page :show

  def show
    valid = %w(static1 static2 static3)
    if valid.include?(params[:path])
      render :template => File.join('static', params[:path])
    else
      render :file   => File.join(Rails.root, 'public', '404.html'), 
             :status => 404
    end
  end
end

Lastly, we'll need to define a route.

# config/routes.rb
map.connect 'static/:path', :controller => 'static', :action => 'show'

Try accessing these static pages. If the path doesn't include a valid template, we'll render the 404 file and return a 404 status.

If you take a look in app/public you'll notice a static/ directory with static1.html, static2.html and static3.html. After accessing the page for the first time, any subsequent requests will be entirely static thanks to page caching.

Tate Johnson
+1  A: 

Considering if u have 1 Home Controller with couple method like show, aboutus, privacy :

class HomesController < ApplicationController
  def show
  end
  def privacy
  end
  def aboutus
  end
end

And map the show method to your root, and map the other to some named routes like

map.root      :controller => "homes", :action => "show"
map.aboutus "/aboutus", :controller => "homes", :action => "aboutus"
map.privacy "/privacy", :controller => "homes", :action => "privacy"

And with view for each

app/views/homes/aboutus.html.erb --> you get http://localhost:3000/aboutus
app/views/homes/show.html.erb --> you get http://localhost:3000 (root)
app/views/homes/privacy.html.erb --> you get http://localhost:3000/privacy

All using the same layout at app/views/layout/application.html.erb

gkrdvl
+11  A: 

depends on the url structure, if you want the paths to come off of / (e.g. /about_us), then:

map.connect ':action', :controller => "static"

This should go at the very end of your routes file, Throw your .html.erb files into app/views/static and you are done.

e.g: throwing in about_us.html.erb, will give you a page at /about_us.

The item that you have in your question is great for a catch all route where you can analyze the array given to you at params[:path]. A bit more information on that at http://railscasts.com/episodes/46-catch-all-route

Omar Qureshi
+2  A: 

thoughtbot has a plugin called high_voltage for displaying static content: http://github.com/thoughtbot/high_voltage

Gabe Martin-Dempesy
broken link: use: http://github.com/thoughtbot/high_voltage
Jonathan
Just added this to a project and it works great!
Abel Martin