简体   繁体   中英

What's the best way to handle nested static pages in rails?

EDIT: Sorry, the title is a little unclear, I wanted to use 'semi-static' pages, using the render helper and ruby vars. The ERB templating system etc. Sorry guys! My fault!

I've been looking into creating nested semi-static pages for a rails 3.1.3 app I'm building, and I've yet to find an answer that would suit all my needs. Any ideas?

All of the solutions I've come across are about creating just top level pages, like so:

- Site root
--- About (http://wwww.example.com/about)
--- Contact (http://wwww.example.com/contact)
--- Products (http://wwww.example.com/products)
--- Pricing (http://wwww.example.com/pricing

Whereas I'm looking to do something like

- Site root
--- About (http://wwww.example.com/about)
------ What we do (http://wwww.example.com/about/what-we-do)
------ Another sub-page (http://wwww.example.com/about/another-sub-page)
--- Contact (http://wwww.example.com/contact)
--- Products (http://wwww.example.com/products)
------ Product One (http://wwww.example.com/products/product-one)
------ Product Two (http://wwww.example.com/products/product-two)
--- Pricing (http://wwww.example.com/pricing)

I've come across solutions like mapping static controllers for each of the pages, but that doesn't seem like a particularly elegant solution.

Or creating a generic route and controller to match requests, like so:

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

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

But that seems even more inelegant, is there another way I'm missing?

is there another way I'm missing?

Yes.

All you have to do is create static pages as you require in /public, either in the root of public , or in a directory structure.

A physical file existing at a path under /public should override any routes you configure to dynamically generated pages.

I faced a similar problem for static sub pages, came up with the following solution.

In routes.rb

match 'about/(:page)', :to => 'about#show', as: 'about'
match 'about/what-we-do', :to => 'about#show', as: 'what_we_do'

In about_controller.rb

class AboutController < ApplicationController
  def show
    unless params[:page].blank?
      render "about/#{params[:page].underscore}"
    end
  end
end

In your views you can reference the alias paths.

<%= link_to 'About', about_path %>
<%= link_to 'What We Do', what_we_do_path %>

So /about will default to rendering the view about.html.erb .

But /about/what-we-do will render the view about/what_we_do.html.erb .

Would something like this help solve your problem?

What DanSingerman said, but also... Just put your static pages on a separate fqdn, possibly hosted on a cdn. The only reason to have rails serve static assets is that you're being lazy and just can't be bothered doing it the right way.

That is in fact what most CMS's do. If you feel that you are doing a lot of heavy lifting try plugging in a CMS like Refinery to your app. It makes life a little simpler by taking care of some of the SEO aspects. If you are interested in how Refinery CMS handles its pages, have a look at the Pages Controller and the related routes and the magic match all route .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM