Use Rails to Render Default Index Page #8468

Merged
merged 1 commit into from Dec 11, 2012
@schneems
Ruby on Rails member

This is an alternative implementation to #7771 thanks to the advice of @spastorino

Rails is a dynamic framework that serves a static index.html by default. One of my first questions ever on IRC was solved by simply deleting my public/index.html file. 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.

This PR moves the static index page to an internal controller/route/view similar to rails/info. When someone starts a rails server, if no root is defined, this route will take over and the "dynamic" index page from rails/welcome_controller will be rendered. These routes are only added in development. If a developer defines a root in their routes, it automatically takes precedence over this route and will be rendered, with no deleting of files required.

In addition to removing this source of confusion for new devs, we can now use Rails view helpers to build and render this page. While not the primary intent, the added value of "dogfooding" should not be under-estimated.

The prior PR #7771 had push-back since it introduced developer facing files. This PR solves all of the same problems, but does not have any new developer facing files (it actually removes one).

cc/ @wsouto, @dickeyxxx, @tyre, @ryanb, @josevalim, @maxim, @subdigital, @steveklabnik

ATP Railties and Actionpack.

@robin850
Ruby on Rails member

Just giving a big 👍 !

@sxua

👍 * 💯

@steveklabnik
Ruby on Rails member

:shipit: as far as I'm concerned. This is great.

@guilleiguaran
Ruby on Rails member

This is a big improvement.

👍 on my side

@nthj

👍 yes!

@mgonto

👍 finally!

@mgonto mgonto and 4 others commented on an outdated diff Dec 9, 2012
actionpack/lib/action_dispatch/routing/inspector.rb
@@ -51,7 +51,8 @@ def action
end
def internal?
- path =~ %r{/rails/info.*|^#{Rails.application.config.assets.prefix}}
+ return true if controller =~ %r{^rails/info|^rails/welcome}
@mgonto
mgonto added a note Dec 9, 2012

Why return true if... instead of just controller=~.... OR path=~.... I think that's clearer and no return needed

@steveklabnik
Ruby on Rails member

Only bad part is that it makes a damn long line.

@carlosantoniodasilva
Ruby on Rails member

How about:

controller =~ %r{^rails/info|^rails/welcome} || 
  path =~ %r{^#{Rails.application.config.assets.prefix}}
@schneems
Ruby on Rails member
schneems added a note Dec 9, 2012

I still prefer explicit returns here, it also gives us a true return instead of a truthy return. If you prefer a different style I can change it up.

@spastorino
Ruby on Rails member

👍 to what @carlosantoniodasilva said

@carlosantoniodasilva
Ruby on Rails member

Btw, for a true return you could use ===:

%r{^rails/info|^rails/welcome} === controller || 
  %r{^#{Rails.application.config.assets.prefix}} === path

Not that it'd make any big difference, just in case :).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@agis-

👍 no doubt

@carlosantoniodasilva carlosantoniodasilva and 1 other commented on an outdated diff Dec 9, 2012
railties/lib/rails/welcome_controller.rb
@@ -0,0 +1,7 @@
+class Rails::WelcomeController < ActionController::Base
+ self.view_paths = File.join(File.dirname(__FILE__), 'templates')
@carlosantoniodasilva
Ruby on Rails member

You could use File.expand_path here instead.

@schneems
Ruby on Rails member
schneems added a note Dec 9, 2012

Good idea, I can change the Rails::InfoController as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@carlosantoniodasilva
Ruby on Rails member

Looks good to me as well. Just as reminder, we need to revert the flag to skip the index page.

@gaurish

👍

About serving the index page in production,
I think should enable this index page in production if no root route is provided because

  • there is no harm in doing so: yes, might expose information about an application,but if an app doesn't have a root path, it probably means developer is just testing anyway. In that that additional information might be helpful.
  • behavior in production & development environment should consistent:, unless there is a strong reason to do so. I can imagine someone new to rails generating new app, testing it locally & deploying it. only to encounter that dreaded "something went wrong" page & left wondering, why does it work in development & fails in production? The principle of least astonishment (POLA/PLA)
  • Hosting Providers & sysadmin can use this page to quickly troubleshoot errors with production apps. For example, if you are getting "something went wrong" page even without having a root path, means something is wrong with server configuration(missing ruby etc) but getting that default index.html page means everything is okay.

so I think we should display this page in production, if route path is not defined.

@schneems
You might want to edit that index.html & remove the lines instructing to delete index.html as that doesn't makes sense.

@phawk

Totally agree that this would take away a small pain point for intermediate / expert users of rails. I myself would benefit from it.

However, there is one thing we are taking away from users new to the framework, having this index.html file in the public folder teaches new users that static files override rails routes.

We need to decide if this lesson is less valuable than the pain it causes. Personally I think this lesson is more important that the pain it causes me.

@schneems
Ruby on Rails member

@phawk a better way to show that would be to include HTML files in public in the output of 'rake routes'.

@georgeclaghorn

Definitely support dynamically rendering the default index, but I don't agree that it should be rendered in production. The existing /rails/info routes are only visible in development. We also don't render app error details in production.

It's not necessary to have consistent behavior between the development and production environments. They are separate environments for a reason.

@carlosantoniodasilva
Ruby on Rails member

There's no possibility for this being added to environments other than development.

@schneems
Ruby on Rails member

Switched to expand_path and added a changelog.

@zires

👍 thanks for saving our time

@rafaelfranca
Ruby on Rails member

@schneems would be great a test to ensure that a root route will take precedence.

@rafaelfranca
Ruby on Rails member

@phawk @schneems the best way to teach this is on the guides.

@schneems
Ruby on Rails member

@rafaelfranca added at test for the root route to take precedence in development

@rafaelfranca
Ruby on Rails member

:shipit:

@MJIO

👍

@spastorino
Ruby on Rails member

This seems good to me, as @carlosantoniodasilva noted we will need to remove the flag to skip the index before merging. Thanks for working on this.

@frodsan

Could you update Setting the Application Home Page section? 👍

@schneems
Ruby on Rails member

Updated the internal? method and modified the docs.

@gaurish

the page still says remove public/index.html. you might want to edit the index page

@schneems
Ruby on Rails member

Thanks @gaurish I totally wasn't accidentally leaving that in there for an easy PR later or anything 🎱

@schneems
Ruby on Rails member

updated to remove that line

@georgeclaghorn

Maybe instead of removing that line, you could just change it to "Set up a root route to replace this default homepage" or something similar for clarity?

@schneems
Ruby on Rails member

bikeshed, merge the PR and we'll change it later. Also, I would 👍 that change.

@carlosantoniodasilva
Ruby on Rails member

Looks good to me 👍, thanks @schneems.

@spastorino all yours :)

@carlosantoniodasilva
Ruby on Rails member

Actually, @schneems weren't you going to remove that skip_index_html flag from the generator? You should be able to revert 5c1109a.

@schneems
Ruby on Rails member

@carlosantoniodasilva yes, i can do that, didn't realize you wanted me to manually revert in same PR. I'll work on that.

@carlosantoniodasilva
Ruby on Rails member

@schneems I think we'll get rid of the option anyway, so it's probably better to do it together :)

@schneems
Ruby on Rails member

I updated pulled out the skip-index stuffs

@frodsan frodsan commented on the diff Dec 10, 2012
railties/lib/rails/welcome_controller.rb
@@ -0,0 +1,7 @@
+class Rails::WelcomeController < ActionController::Base
@frodsan
frodsan added a note Dec 10, 2012

Could you # :nodoc: this?

@frodsan
frodsan added a note Dec 10, 2012

Also, please can you nodoc Rails::InfoController? 😄

@schneems
Ruby on Rails member

done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@schneems schneems Use Rails to Render Default Index Page
This is an alternative implementation to #7771 thanks to the advice of @spastorino

Rails is a dynamic framework that serves a static index.html by default. One of my first questions ever on IRC was solved by simply deleting my public/index.html file. 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.

This PR moves the static index page to an internal controller/route/view similar to `rails/info`. When someone starts a rails server, if no root is defined, this route will take over and the "dynamic" index page from rails/welcome_controller will be rendered. These routes are only added in development. If a developer defines a root in their routes, it automatically takes precedence over this route and will be rendered, with no deleting of files required. 

In addition to removing this source of confusion for new devs, we can now use Rails view helpers to build and render this page. While not the primary intent, the added value of "dogfooding" should not be under-estimated.

The prior PR #7771 had push-back since it introduced developer facing files. This PR solves all of the same problems, but does not have any new developer facing files (it actually removes one). 

cc/ @wsouto, @dickeyxxx, @tyre, @ryanb, @josevalim, @maxim, @subdigital, @steveklabnik

ATP Railties and Actionpack.
baea5d6
@carlosantoniodasilva
Ruby on Rails member

❤️

@schneems
Ruby on Rails member

anything else?

@carlosantoniodasilva
Ruby on Rails member

I guess not, lets just wait for @spastorino to do a final check. Thanks @schneems.

@spastorino spastorino merged commit 603e7f7 into rails:master Dec 11, 2012
@rbq

Yay, finally. :)

@gregstallings

So we've now replaced having to delete a public/index.html file with having to delete a welcome_controller and a view?

@sxua

@gregstallings WelcomeController is inside of railties, you don't have to delete it, all you need is to point root in routes.rb to your controller. :shipit:

@rafaelfranca
Ruby on Rails member

@gregstallings no. You only need to define a root route and the default index action will be replaced.

@johnrlive

Awesome as a newbie this always bugged me. It's the little things that count!

@ravidsrk

Awesome!

@scalabl3

+2!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment