No change to website output after updating Rails project

59 Views Asked by At

I am attempting to get Rails to output "Hello World" by following the directions in https://guides.rubyonrails.org/getting_started.html. Starting off with an empty Rails project, I run the server and the local host page shows the Rails logo, as expected. The html address is http://, followed by a 3-digit number, a decimal, a 1-digit number, a decimal, the same 1-digit number, a decimal, a different 1-digit number, a colon and a 4-digit number. Next, I add or edit the following files and restart the server. However, there is no change in the local server page, even when I add "/articles" to the end of the html address on the local host page. (That said, the page doesn't crash, so I think I can rule out an error as opposed to a bug.)

config/routes.rb:

Rails.application.routes.draw do
  # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

  # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
  # Can be used by load balancers and uptime monitors to verify that the app is live.
  get "up" => "rails/health#show", as: :rails_health_check

  # Defines the root path route ("/")
  # root "posts#index"
  get "/articles", to: "articles#index"

end

app/controllers/articles_controller.rb:

class ArticlesController < ApplicationController
  def index
  end
end

app/views/articles/index.html.erb:

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <style>
      /* Email styles need to be inline */
  </style>
</head>

<body>
  <h1>Hello, Rails!</h1>
</body>
</html>
1

There are 1 best solutions below

1
Allison On

First, some basic context... the URL is http://127.0.0.1:3000/... here's a generalized breakdown on how these concepts work (please don't hit me with pedantic comments).

A basic URL is structured like this (see the URL RFC): //<user>:<password>@<host>:<port>/<url-path> You might not be familiar with some of these things because they're typically omitted from the kinds of URLs a normal internet user is typing or observing in their address bar. The URLs https://spaghetti:[email protected]:443 and http://spaghetti:[email protected]:80 will both take you to google.com in a web browser, but they're not the thing you're used to typing to get to Google Search. They work because the username and password arguments aren't needed to access the site, your DNS server is able to translate the host google.com to the IP of Google's remote host on the internet, 443 is the default networking port for https requests, and 80 is the default networking port for http requests.

When you type google.com into your modern browser, it should interpret that host argument and assume it needs to use the https protocol (rules describing how data is transmitted across a network). The https protocol uses the https service to securely connect to the website via port 443, which is the port registered for the https service in the IANA Port Number Registry.

When your browser sees 127.0.0.1 (or localhost), unless you've badly mucked with your hostfile it knows that the host is your local machine. The default rails server port is 3000, so the address http://127.0.0.1:3000 is telling your browser 'use the http protocol to take me to port 3000 on my local machine'.

Your Actual Problem

Your rails server root is running at http://127.0.0.1:3000/, but your routes file doesn't define any routes that would direct traffic hitting that address to a controller action. If root 'posts#index' were not commented out, it would direct traffic hitting the root to the index action inside your app's PostsController. Based on the routes file you've provided, you're going to need to go to http://127.0.0.1:3000/articles to hit ArticlesController#index; when you go to that address, you should see the request reflected in your terminal tab where you're running your rails server. If the address doesn't chain :3000 after the host (before the path), your browser will probably default to trying to take you to a resource at port 80 (which is the wrong port for your server).

If you're going to that address and still not seeing anything, the server output in your terminal should be showing you a running log of the traffic your server is receiving and any errors (e.g., the controller action method you're routing to is not defined, the request is hitting your controller action but when it tries to render at the end it can't find the file where you've defined your view, etc.

Read the rails guides closely, they are really well-written and cover everything you need to know to go a long way with rails.