Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make Default Index Page Dynamic

Rails is a dynamic framework that serves a static `index.html` by default. One of my first questions ever on IRC was solved by "delete your index.html". This file is a source of confusion when starting as it over-rides any set "root" in the routes yet it itself is not listed in the routes. By making the page dynamic by default we can eliminate this confusion and show people how to set the root on their project all at the same time.

I encounter this question any time I am helping someone new learn Rails, and it is advice repeated again and again from our comments in rails/rails to external tutorials. We can remove this extra step and show people how to use rails root in the routes by allowing the index to be dynamic. 

This makes it easier to understand how to replace the root if desired, or to modify the index once generated since all rails helper methods are available. Eventually we can refactor this page to use standard Rails helpers instead of pure static html.

Screenshot:

![](http://f.cl.ly/items/38212K0w1P063u2I0g2E/Screen%20Shot%202012-09-25%20at%204.51.42%20PM.png)
  • Loading branch information...
commit 0bd6fa270b405d0f35031603b6dfce89b8aea055 1 parent 5f15956
@schneems authored
View
4 railties/CHANGELOG.md
@@ -1,5 +1,9 @@
## Rails 4.0.0 (unreleased) ##
+* Move `public/index.html` to a dynamic page `app/views/pages/index.html.erb`.
+
+ *Richard Schneeman*
+
* Engines with a dummy app include the rake tasks of dependencies in the app namespace.
Fix #8229
View
7 railties/lib/rails/generators/rails/app/templates/app/controllers/pages_controller.rb
@@ -0,0 +1,7 @@
+class PagesController < ApplicationController
+
+ def index
+ render layout: false
+ end
+
+end
View
0  .../generators/rails/app/templates/public/index.html → ...ails/app/templates/app/views/pages/index.html.erb
File renamed without changes
View
5 railties/lib/rails/generators/rails/app/templates/config/routes.rb
@@ -2,8 +2,9 @@
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
- # You can have the root of your site routed with "root" just remember to delete public/index.html.
- # root to: 'welcome#index'
+
+ # You can have the root of your site routed with "root"
+ root to: 'pages#index'
# Example of regular route:
# get 'products/:id' => 'catalog#view'
View
2  railties/test/generators/app_generator_test.rb
@@ -55,7 +55,7 @@ def test_assets
assert_file "app/views/layouts/application.html.erb", /javascript_include_tag\s+"application"/
assert_file "app/assets/stylesheets/application.css"
assert_file "config/application.rb", /config\.assets\.enabled = true/
- assert_file "public/index.html", /url\("assets\/rails.png"\);/
+ assert_file "app/views/pages/index.html.erb", /url\("assets\/rails.png"\);/
end
def test_invalid_application_name_raises_an_error
Please sign in to comment.
Something went wrong with that request. Please try again.