Le rendu d'une action n'a pas de sens. Vous aurez envie de rendre un template (ou un fichier) avec une mise en page.
# 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
Vous pouvez servir une variété de différents modèles à partir d'une seule action avec la page en cache.
# 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
Enfin, nous avons besoin de définir un itinéraire.
# config/routes.rb
map.connect 'static/:path', :controller => 'static', :action => 'show'
Essayez d'accéder à ces pages statiques. Si le chemin d'accès ne comprend pas un modèle valide, nous allons le rendre le 404 fichier et renvoyer une 404 état.
http://localhost:3000/static/static1
http://localhost:3000/static/static3
http://localhost:3000/static/static2
Si vous jetez un oeil dans app/public, vous remarquerez une statique/ répertoire avec static1.html, static2.html et static3.html. Après avoir accédé à la page pour la première fois, toutes les demandes subséquentes seront entièrement statique grâce à la page en cache.