5

What's the best way to serve static HTML documents in Rails with a layout? Obviously I could just keep the HTML files in the public/ directory, but then I wouldn't be able to apply a layout, or could I? Otherwise I could put the following in config/routes.rb:

match ':page' => 'static#display', :page => /.+\.html/

Does .+\.html work so it ends with .html? Anyway, assuming it did, I guess I'd have a controller:

class StaticController < ApplicationController
  layout 'static_files'
  def display
    render params[:page]
  end
end

Assuming that works properly, will Ruby unnecessarily try and parse the HTML file as an ERB file? Is there a better mechanism Rails has for this?

1
  • From what I understand it seems like Rails will parse the .html files through ERB if the files have the .html.erb extension. Commented Sep 26, 2012 at 18:52

4 Answers 4

5

This tutorial has a pretty good explanation of static pages in rails.

First you can generate the static pages via the rails generator:

rails generate controller StaticPages home help --no-test-framework

Then you can edit your config/routes.rb to look like the following:

SampleApp::Application.routes.draw do
  root to: 'static_pages#home'
  match '/help',    to: 'static_pages#help'
  .
  .
  .
end

And finally in your StaticPages controller you create the home and help methods.

class StaticPagesController < ApplicationController
  def home
  end

  def help
  end
end

UPDATE - quote source: Ruby on Rails Guides - Section 2.1

You’ve heard that Rails promotes “convention over configuration”. Default rendering is an excellent example of this. By default, controllers in Rails automatically render views with names that correspond to valid routes. For example, if you have this code in your BooksController class:

class BooksController < ApplicationController 
  #empty controller
end 

And the following in your routes file:

resources :books 

And if you have a view file app/views/books/index.html.erb, then Rails will render it even without the methods. This should work for any mapped routes/equivalent views.

Sign up to request clarification or add additional context in comments.

1 Comment

I don't want to create 85 methods in the pages controller for my 85 static html pages. Like my example above, I want a catch-all route going to a single controller action at most.
2

You could use the high_voltage gem by Thoughtbot. It's purpose is to include static pages in Rails, and you use any templating language you like. Internally it works similarly to the solutions proposed here.

You can customize the layout file for every single page served by high_voltage as described here in the Readme.

1 Comment

This looks perfect, trying this now.
0

Create a controller for handling static pages

rails generate controller pages

Map routes to these actions in routes.rb

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

Create actions in pages controller

def home
CODE
end

def contact
CODE
end

def help
CODE
end

By default, rails will look for ACTION.html.erb in the view folder. Create views and voila. Let me know if this works for you.

1 Comment

I don't want to create 85 methods in the pages controller for my 85 static html pages. Like my example above, I want a catch-all route going to a single controller action at most.
0

I don't want to create 85 methods in the pages controller for my 85 static HTML pages.

This may seem a little hackish but does exactly what you want:

# File: app/controllers/static_controller.rb

class StaticController < ApplicationController
  def method_missing(m, *args, &block)
  end
end

Now put something under the correct directory, for example app/views/static/example.html.erb, and route to it:

get '/example', to: 'static#example'

You didn't really define an action named method_missing, but something with black magic. There's documentation for this: https://ruby-doc.org/core-2.7.1/BasicObject.html#method-i-method_missing

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.