Suppose you'd like to have some "static" (as in html.erb, without content in the DB) pages on your Rails site. And to have some nice path, like mydomain.com/articles/somearticle, pointing to a file somearticle.html.erb somewhere in your app directory.

Well, this is the simplest and probably nicest of the options (and it's very "Ruby Way"). But I have to test it a bit more to clear some security questions - use at your own risk.

class ArticlesController < ApplicationController
  def method_missing(name)
    path = "#{RAILS_ROOT}/public/articles/#{name}.html.erb"
    render :file => path, :layout => true
  end
end

The best thing about it? 100% Globalize compatible, i.e. Globalize will automatically try to prepend extension with two-letter language code as usual with view files.

1 Response to “Serving html(.erb) files from a controller - the easiest method.”

  1. Robert Nasiadek Says:

    Actually, if we’re ok with html files living in the app/views/articles directory, we can make it even simpler by doing it this way:

    def method_missing( name ) render :action => name end

    Didn’t try it out with globalize, but I believe it will work.

Sorry, comments are closed for this article.