Author |
Topic: Rails Probject I -- Hello World |
|
Linux member offline |
|
posts: |
120 |
joined: |
01/24/2011 |
from: |
San Jose, CA |
|
|
|
|
|
Rails Probject I -- Hello World |
Step 1 Create a new Rails project
Rails comes with a number of scripts called generators that are designed to make your development life easier by creating everything that's necessary to start working on a particular task. One of these is the new application generator, which will provide you with the foundation of a fresh Rails application so that you don't have to write it yourself.
-- administrator@ubuntu:~$ rails new hello_world
create
create README.md
create Rakefile
create config.ru
create .gitignore
create Gemfile
run git init from "."
Initialized empty Git repository in /home/administrator/hello_world/.git/
create app
create app/assets/config/manifest.js
create app/assets/javascripts/application.js
create app/assets/javascripts/cable.js
create app/assets/stylesheets/application.css
create app/channels/application_cable/channel.rb
create app/channels/application_cable/connection.rb
create app/controllers/application_controller.rb
create app/helpers/application_helper.rb
create app/jobs/application_job.rb
create app/mailers/application_mailer.rb
create app/models/application_record.rb
create app/views/layouts/application.html.erb
create app/views/layouts/mailer.html.erb
create app/views/layouts/mailer.text.erb
create app/assets/images/.keep
create app/assets/javascripts/channels
create app/assets/javascripts/channels/.keep
create app/controllers/concerns/.keep
create app/models/concerns/.keep
create bin
create bin/bundle
create bin/rails
create bin/rake
create bin/setup
create bin/update
create bin/yarn
create config
create config/routes.rb
create config/application.rb
create config/environment.rb
create config/secrets.yml
create config/cable.yml
create config/puma.rb
create config/spring.rb
create config/environments
create config/environments/development.rb
create config/environments/production.rb
create config/environments/test.rb
create config/initializers
create config/initializers/application_controller_renderer.rb
create config/initializers/assets.rb
create config/initializers/backtrace_silencers.rb
create config/initializers/cookies_serializer.rb
create config/initializers/cors.rb
create config/initializers/filter_parameter_logging.rb
create config/initializers/inflections.rb
create config/initializers/mime_types.rb
create config/initializers/new_framework_defaults_5_1.rb
create config/initializers/wrap_parameters.rb
create config/locales
create config/locales/en.yml
create config/boot.rb
create config/database.yml
create db
create db/seeds.rb
create lib
create lib/tasks
create lib/tasks/.keep
create lib/assets
create lib/assets/.keep
create log
create log/.keep
create public
create public/404.html
create public/422.html
create public/500.html
create public/apple-touch-icon-precomposed.png
create public/apple-touch-icon.png
create public/favicon.ico
create public/robots.txt
create test/fixtures
create test/fixtures/.keep
create test/fixtures/files
create test/fixtures/files/.keep
create test/controllers
create test/controllers/.keep
create test/mailers
create test/mailers/.keep
create test/models
create test/models/.keep
create test/helpers
create test/helpers/.keep
create test/integration
create test/integration/.keep
create test/test_helper.rb
create test/system
create test/system/.keep
create test/application_system_test_case.rb
create tmp
create tmp/.keep
create tmp/cache
create tmp/cache/assets
create vendor
create vendor/.keep
create package.json
remove config/initializers/cors.rb
remove config/initializers/new_framework_defaults_5_1.rb
run bundle install
This will create a Rails application called hello_world in a hello_world directory with all necessary folders and files generated.
Among them, the most important ones are: config -- the directory with configuration files, e.g. routes.rb app/controllers-- the directory holding controllers app/models-- the directory holding models app/views-- the directory holding views
|
|
|
|
|
|
|
Linux member offline |
|
posts: |
120 |
joined: |
01/24/2011 |
from: |
San Jose, CA |
|
|
|
|
|
What's going on? |
Without your touch, your default empty web application is ready to go online.
First start the server, which is a built-in Puma.
-- administrator@ubuntu:~/hello_world$ rails server
=> Booting Puma
=> Rails 5.1.4 application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.10.0 (ruby 2.4.0-p0), codename: Russell's Teapot
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
Secondly, on your browser, type:
Yay! You’re on Rails!
Rails version: 5.1.4
Ruby version: 2.4.0 (x86_64-linux)
It is online! But where is the "Welcome aboard" index page coming from? What's going on here?
First take a look at the routing table: "/config/routes.rb"
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
It's empty and the deafult should be kicked in. Take a look at the traffic log
Started GET "/" for 127.0.0.1 at 2017-09-10 14:17:31 -0700
Processing by Rails::WelcomeController#index as HTML
Rendering /home/administrator/.rvm/gems/ruby-2.4.0/gems/railties-5.1.4/lib/
rails/templates/rails/welcome/index.html.erb
Rendered /home/administrator/.rvm/gems/ruby-2.4.0/gems/railties-5.1.4/lib/
rails/templates/rails/welcome/index.html.erb (4.3ms)
Completed 200 OK in 198ms (Views: 10.0ms)
As the log indicates, the "Welcome aboard" page is coming from "lib/rails/templates/rails/welcome/index.html.erb"
|
|
|
|
|
|
|
Linux member offline |
|
posts: |
120 |
joined: |
01/24/2011 |
from: |
San Jose, CA |
|
|
|
|
|
Step 2 Make the minimum change |
Rails framework follows the MVC philosophy, but here the "Hello World" is the simplest one which can only the C (controller) component.
First tale a look at the default controller: "app/controllers/application_controller.rb"
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
end
Change it to:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
def greet
render html: "Hello, World!"
end
end
Then, instruct the routing table "config/routes.rb" to use the modified controller:
Rails.application.routes.draw do
# For details on the DSL available within this file,
see http://guides.rubyonrails.org/routing.html
root 'application#greet'
end
Here, the 'root' (/) request will be handled by "application" controller (ApplicationController) on "greet" action.
|
|
|
|
|
|
|
Linux member offline |
|
posts: |
120 |
joined: |
01/24/2011 |
from: |
San Jose, CA |
|
|
|
|
|
Proof from traffic log |
Started GET "/" for 127.0.0.1 at 2017-09-10 15:12:14 -0700
Processing by ApplicationController#greet as HTML
Rendering html template
Rendered html template (0.0ms)
Completed 200 OK in 170ms (Views: 4.8ms)
|
|
|
|
|
|
|
Linux member offline |
|
posts: |
120 |
joined: |
01/24/2011 |
from: |
San Jose, CA |
|
|
|
|
|
|