go to  ForumEasy.com   
JavaPro
Home » Archive » Message


[Email To Friend][View in Live Context][prev topic « prev post | next post » next topic]
  Step 2 Make the minimum change
 
Subject: Step 2 Make the minimum change
Author: Linux
In response to: What's going on?
Posted on: 09/10/2017 10:10:14 PM

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.


 

> On 09/10/2017 09:35:13 PM Linux wrote:

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:
http://localhost:3000


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"





References:

 


 
Powered by ForumEasy © 2002-2022, All Rights Reserved. | Privacy Policy | Terms of Use
 
Get your own forum today. It's easy and free.