mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #8468 from schneems/schneems/rack-index-page
Use Rails to Render Default Index Page
This commit is contained in:
commit
603e7f7ea5
12 changed files with 55 additions and 28 deletions
|
@ -51,7 +51,7 @@ module ActionDispatch
|
|||
end
|
||||
|
||||
def internal?
|
||||
path =~ %r{/rails/info.*|^#{Rails.application.config.assets.prefix}}
|
||||
controller =~ %r{^rails/info|^rails/welcome} || path =~ %r{^#{Rails.application.config.assets.prefix}}
|
||||
end
|
||||
|
||||
def engine?
|
||||
|
|
|
@ -215,11 +215,7 @@ Open the `app/views/welcome/index.html.erb` file in your text editor and edit it
|
|||
|
||||
### Setting the Application Home Page
|
||||
|
||||
Now that we have made the controller and view, we need to tell Rails when we want Hello Rails! to show up. In our case, we want it to show up when we navigate to the root URL of our site, <http://localhost:3000>. At the moment, however, the "Welcome Aboard" smoke test is occupying that spot.
|
||||
|
||||
To fix this, delete the `index.html` file located inside the `public` directory of the application.
|
||||
|
||||
You need to do this because Rails will serve any static file in the `public` directory that matches a route in preference to any dynamic content you generate from the controllers. The `index.html` file is special: it will be served if a request comes in at the root route, e.g. <http://localhost:3000>. If another request such as <http://localhost:3000/welcome> happened, a static file at `public/welcome.html` would be served first, but only if it existed.
|
||||
Now that we have made the controller and view, we need to tell Rails when we want Hello Rails! to show up. In our case, we want it to show up when we navigate to the root URL of our site, <http://localhost:3000>. At the moment, "Welcome Aboard" is occupying that spot.
|
||||
|
||||
Next, you have to tell Rails where your actual home page is located.
|
||||
|
||||
|
@ -233,7 +229,6 @@ Blog::Application.routes.draw do
|
|||
# first created -> highest priority.
|
||||
# ...
|
||||
# You can have the root of your site routed with "root"
|
||||
# just remember to delete public/index.html.
|
||||
# root to: "welcome#index"
|
||||
```
|
||||
|
||||
|
@ -558,7 +553,7 @@ parameter, which in our case will be the id of the post. Note that this
|
|||
time we had to specify the actual mapping, `posts#show` because
|
||||
otherwise Rails would not know which action to render.
|
||||
|
||||
As we did before, we need to add the `show` action in
|
||||
As we did before, we need to add the `show` action in
|
||||
`app/controllers/posts_controller.rb` and its respective view.
|
||||
|
||||
```ruby
|
||||
|
|
|
@ -1,9 +1,13 @@
|
|||
## Rails 4.0.0 (unreleased) ##
|
||||
|
||||
* The public/index.html is no longer generated for new projects. Page is replaced by internal welcome_controller inside of railties
|
||||
|
||||
*Richard Schneeman*
|
||||
|
||||
* Add ENV['RACK_ENV'] support to `rails runner/console/server`.
|
||||
|
||||
*kennyj*
|
||||
|
||||
|
||||
* Add `db` to list of folders included by `rake notes` and `rake notes:custom`. *Antonio Cangiano*
|
||||
|
||||
* Engines with a dummy app include the rake tasks of dependencies in the app namespace.
|
||||
|
|
|
@ -21,7 +21,8 @@ end
|
|||
|
||||
module Rails
|
||||
autoload :Info, 'rails/info'
|
||||
autoload :InfoController, 'rails/info_controller'
|
||||
autoload :InfoController, 'rails/info_controller'
|
||||
autoload :WelcomeController, 'rails/welcome_controller'
|
||||
|
||||
class << self
|
||||
attr_accessor :application, :cache, :logger
|
||||
|
|
|
@ -25,6 +25,7 @@ module Rails
|
|||
get '/rails/info/properties' => "rails/info#properties"
|
||||
get '/rails/info/routes' => "rails/info#routes"
|
||||
get '/rails/info' => "rails/info#index"
|
||||
get '/' => "rails/welcome#index"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -52,9 +52,6 @@ module Rails
|
|||
class_option :skip_javascript, type: :boolean, aliases: '-J', default: false,
|
||||
desc: 'Skip JavaScript files'
|
||||
|
||||
class_option :skip_index_html, type: :boolean, aliases: '-I', default: false,
|
||||
desc: 'Skip public/index.html and app/assets/images/rails.png files'
|
||||
|
||||
class_option :dev, type: :boolean, default: false,
|
||||
desc: "Setup the #{name} with Gemfile pointing to your Rails checkout"
|
||||
|
||||
|
|
|
@ -97,11 +97,6 @@ module Rails
|
|||
|
||||
def public_directory
|
||||
directory "public", "public", recursive: false
|
||||
if options[:skip_index_html]
|
||||
remove_file "public/index.html"
|
||||
remove_file 'app/assets/images/rails.png'
|
||||
keep_file 'app/assets/images'
|
||||
end
|
||||
end
|
||||
|
||||
def script
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
# 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.
|
||||
# You can have the root of your site routed with "root"
|
||||
# root to: 'welcome#index'
|
||||
|
||||
# Example of regular route:
|
||||
|
|
|
@ -223,7 +223,6 @@
|
|||
</li>
|
||||
|
||||
<li>
|
||||
<h2>Set up a default route and remove <span class="filename">public/index.html</span></h2>
|
||||
<p>Routes are set up in <span class="filename">config/routes.rb</span>.</p>
|
||||
</li>
|
||||
|
7
railties/lib/rails/welcome_controller.rb
Normal file
7
railties/lib/rails/welcome_controller.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
class Rails::WelcomeController < ActionController::Base # :nodoc:
|
||||
self.view_paths = File.expand_path('../templates', __FILE__)
|
||||
layout nil
|
||||
|
||||
def index
|
||||
end
|
||||
end
|
|
@ -15,6 +15,12 @@ module ApplicationTests
|
|||
teardown_app
|
||||
end
|
||||
|
||||
test "rails/welcome in development" do
|
||||
app("development")
|
||||
get "/"
|
||||
assert_equal 200, last_response.status
|
||||
end
|
||||
|
||||
test "rails/info/routes in development" do
|
||||
app("development")
|
||||
get "/rails/info/routes"
|
||||
|
@ -27,6 +33,36 @@ module ApplicationTests
|
|||
assert_equal 200, last_response.status
|
||||
end
|
||||
|
||||
test "root takes precedence over internal welcome controller" do
|
||||
app("development")
|
||||
|
||||
get '/'
|
||||
assert_match %r{<h1>Getting started</h1>} , last_response.body
|
||||
|
||||
controller :foo, <<-RUBY
|
||||
class FooController < ApplicationController
|
||||
def index
|
||||
render text: "foo"
|
||||
end
|
||||
end
|
||||
RUBY
|
||||
|
||||
app_file 'config/routes.rb', <<-RUBY
|
||||
AppTemplate::Application.routes.draw do
|
||||
root to: "foo#index"
|
||||
end
|
||||
RUBY
|
||||
|
||||
get '/'
|
||||
assert_equal 'foo', last_response.body
|
||||
end
|
||||
|
||||
test "rails/welcome in production" do
|
||||
app("production")
|
||||
get "/"
|
||||
assert_equal 404, last_response.status
|
||||
end
|
||||
|
||||
test "rails/info/routes in production" do
|
||||
app("production")
|
||||
get "/rails/info/routes"
|
||||
|
|
|
@ -55,7 +55,6 @@ class AppGeneratorTest < Rails::Generators::TestCase
|
|||
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"\);/
|
||||
end
|
||||
|
||||
def test_invalid_application_name_raises_an_error
|
||||
|
@ -251,13 +250,6 @@ class AppGeneratorTest < Rails::Generators::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
def test_generator_if_skip_index_html_is_given
|
||||
run_generator [destination_root, '--skip-index-html']
|
||||
assert_no_file 'public/index.html'
|
||||
assert_no_file 'app/assets/images/rails.png'
|
||||
assert_file 'app/assets/images/.keep'
|
||||
end
|
||||
|
||||
def test_creation_of_a_test_directory
|
||||
run_generator
|
||||
assert_file 'test'
|
||||
|
|
Loading…
Reference in a new issue