Serving static HTML in Rails with a layout file

What is the best way to serve static HTML documents in Rails with layout? Obviously, I could just store the HTML files in a directory public/, but then I could not apply the layout, or could I? Otherwise, I could put in the config/routes.rbfollowing:

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

Does .+\.htmlit work to end in .html? In any case, assuming this is so, I think I will have a controller:

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

Assuming this works correctly, will Ruby unnecessarily try to parse the HTML file as an ERB file? Is there a better way for Rails to do this?

+5
source share
3 answers

high_voltage gem Thoughtbot. , Rails, , . .

, high_voltage, Readme.

+2

tutorial .

:

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

config/routes.rb, :

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

, , StaticPages home help.

class StaticPagesController < ApplicationController
  def home
  end

  def help
  end
end

- : Ruby on Rails Guides - 2.1

, Rails " ". . , Rails , . , BooksController:

class BooksController < ApplicationController 
  #empty controller
end 

:

resources :books 

app/views/books/index.html.erb, Rails . / .

+5

rails generate controller pages

route.rb

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

def home
CODE
end

def contact
CODE
end

def help
CODE
end

ACTION.html.erb . . , .

0

All Articles