Ruby Meets Rails

Rena Zoldan
Rena Adler
Published in
3 min readJul 2, 2017
Photo by Ricardo Gomez Angel on Unsplash

So there’s this thing called Rails, and you won’t BELIEVE what it does!

Rails basically does EVERYTHING, which is really exciting because the thought of manually coding an entire web-app is pretty daunting for a beginner. (Well, it probably not everything but enough for a beginner to get excited about!)

Here’s how you begin your first rails app: (this assumes you have downloaded Ruby and Rails on your computer)

Open your terminal and type the following code:

rails generate controller Pagesrails server

Let’s make sure you are online.

Type localhost:3000 into a browser window. You should see something that looks like this:

Welcome to Rails!

Now, in your text editor, locate your “routes.rb” file. Here’s a cool shortcut you can use:

command + p

This allows you to search through your rails files until you arrive at the correct one. Hit enter and you are good to go!

Type in the following code in your routes.rb file:

Rails.application.routes.draw do

get "/my_file" => "pages#new_method"

Obviously, name your files and methods according to your needs.

Great, you’re all routed. If you were to test it out, you would type localhost:3000/my_file into your browser.

However, you will probably get an error message:

Error Message for GET on Rails

So now you need to create a new method in your controller, so that your router actually knows where to go.

Using the command + p feature, locate your pages_controller.rb file in your rails app. Then, type in:

class PagesController < ApplicationControllerdef new_method
render "hello.html"
end
end

Great, you now have a controller that will run a method, which in this case, will return some awesome html.

But first, you need to create that file.

Go to the folder views > pages and create a new file, named hello.html.

Put whatever you want into it :). I will just write hello, because I don’t want to inhibit your creativity or anything like that.

<h1>Hello!</h1>

There you have it! Now go ahead and test it out. Type in localhost:3000/my_file and you should see:

Your first webpage on Rails!

So, you have successfully given rails a route, which accessed a controller, and that pulled up some HTML, put it to the screen, and the user can now see it!

--

--