Static pages in Ruby on Rails

Ruby on-RailsRuby

Ruby on-Rails Problem Overview


What are the standard way of making a Ruby on Rails application that will have pages such as

  • Home
  • About
  • Contact

I would appricate if someone had links or an answers rather than just say use a gem because I want to learn how to make simple webapps with such behavior.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Depends on how you want to handle the content in those pages.

Approach #1 - store content in views

If you just want to put all your content in ERB views, then a very simple approach is to create a PagesController whose purpose is to deal with static pages. Each page is represented by one action in the controller.

pages_controller.rb:

class PagesController < ApplicationController
  def home
  end

  def about
  end

  def contact
  end
end

routes.rb:

match '/home' => 'pages#home'
match '/about' => 'pages#about'
match '/contact' => 'pages#contact'

Then create home.html.erb, about.html.erb, and contact.html.erb views under app/views/pages. These views contain whatever content you want on your static pages. They'll by default use your app's application.html.erb layout.

You'll also want to look into page caching to give yourself a boost in performance.


Approach #2 - store content in database

Another approach I've used is to make a very basic CMS for static pages. In this case, pages are represented in the model. It uses the friendly_id gem to handle slugs for each page so that they can be retrieved by a pretty name in the URL (e.g., /about) rather than by ID.

page.rb:

class Page < ActiveRecord::Base
  attr_accessible :title, :content
  
  validates_presence_of :title, :content
  
  has_friendly_id :title, :use_slug => true, :approximate_ascii => true
end

pages_controller.rb:

class PagesController < ApplicationController
  def show
    @page = Page.find(params[:id])
    render 'shared/404', :status => 404 if @page.nil?
  end
end

show.html.erb:

<%= raw @page.content %>

routes.rb:

match '/:id' => 'pages#show'

Note: put this entry at the end of routes.rb since it matches everything.

Then how you want to create, edit and update pages are up to you - you can have an admin interface, or build it in to your public interface somehow. This approach can benefit from page caching too.

Solution 2 - Ruby on-Rails

Another option is the high_voltage gem: https://github.com/thoughtbot/high_voltage

This makes it super easy to create static pages where the content is stored in views.

Solution 3 - Ruby on-Rails

Jeff's approach #1 (storing content in views and having a route and controller action for each static page) is a good one. The only thing I would add is to use the controller macro in your routes.

So, instead of this:

match '/home' => 'pages#home'
match '/about' => 'pages#about'
match '/contact' => 'pages#contact'

You can do this:

controller :pages do
  get :home
  get :about
  get :contact
end

It's two extra lines, yet much so much more elegant, since it eliminates repetition and groups your static page routes together visually.

It also uses the get http verb method instead of match, which is a better practice for Rails routes (and more concise, now that Rails 4 requires the http verb to be specified when using match.

Solution 4 - Ruby on-Rails

Check out Michael Hartl's http://railstutorial.org which comes in a 2.3.8 and 3.0.x version. It covers this with great examples and leads you through building them very early on and you will also have the opportunity to learn a lot more than this example. I highly recommend it.

Solution 5 - Ruby on-Rails

Jeff's Approach #1 works great for me. Here is a trick to make the controller dynamically look up pages. With this, you don't need to touch the controller nor the routes.rb for adding pages. Just drop the pages under app/views/pages and the controller will find it.

class PagesController < ApplicationController
  def show
    render params[:id]
  end
end

Solution 6 - Ruby on-Rails

config/routes.rb

get ':id', to: 'pages#show'

app/controllers/pages_controller.rb

class PagesController < ApplicationController
  def show
    begin
      render params[:id]
    rescue ActionView::MissingTemplate
      render :file => "#{Rails.root}/public/404", :layout => false, :status => :not_found
    end
  end
end

Then place your static pages in app/views/pages/{name}.html.erb (or whatever template format.)

Solution 7 - Ruby on-Rails

For more you can create static pages using Jekyll bootstrap or also Jekyll using Danger blog

Refer it is very helpful.

Solution 8 - Ruby on-Rails

An adequate answer to your question would basically look like an introduction to the Rails framework: the MVC structure, templating, and routing DSL at least. Jeff has given a good stab, but his answer still assumes a lot of basic Rails knowledge on your part.

I'd suggest though, that if your webapp is really that simple, Rails might be overkill. I'd look into something lighter, like Sinatra, which has a much lower learning curve than Rails and does a great job of this kind of thing without having to deal with complex routing, magic MVC action/template mapping, etc.

Solution 9 - Ruby on-Rails

I'd suggest adding your pages in the public folder so as to be served directly without having to pass through rails at all. I'm not an expert though so I'm not sure if this could have any cons if the page is static.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLuckyLukeView Question on Stackoverflow
Solution 1 - Ruby on-RailsJeffView Answer on Stackoverflow
Solution 2 - Ruby on-RailsMax MasnickView Answer on Stackoverflow
Solution 3 - Ruby on-RailsRichard JonesView Answer on Stackoverflow
Solution 4 - Ruby on-RailstradayView Answer on Stackoverflow
Solution 5 - Ruby on-RailstimeonView Answer on Stackoverflow
Solution 6 - Ruby on-RailsOwenView Answer on Stackoverflow
Solution 7 - Ruby on-RailsSumit MunotView Answer on Stackoverflow
Solution 8 - Ruby on-RailsDave SimsView Answer on Stackoverflow
Solution 9 - Ruby on-RailsAhmed FathyView Answer on Stackoverflow