Learning Ruby On Rails: Day 9

The tiny “Hello World” app has two pages that link to each other. It works!

Now to break it so I can see why it works.

First I added a typo in the code:

I changed Time.now()(a Ruby method on a Time object) to Time.know()(a nonsense method on a Time object).

In the browser, the following appeared

Then I tried messing with the URL and in the browser. A Routing Error appeared:

These error messages are so helpful and clear. The Routing Error message even has an input field where you can search for the correct path:

What a dream.

So in my first few days of learning Ruby on Rails, I’ve done the following:

  • created a tiny app
  • created a tiny controller, at which time, Rails created a view template for each of the methods inside the tiny controller
  • linked the views together
  • practiced debugging problems in the code and in the URL

It’s been real.

I have a couple Q’s though:

  1. Can I create new methods in the controller in my text editor or do I have to do it via the terminal? I’m pretty sure I know the answer to this, but I like to ask all the questions.
  2. Why doesn’t the following code execute when I put it in the controller?

@addition = <%= 1 + 3 %>

Answer to Question 1: I can create new controller methods in the text editor! But then the view templates aren’t auto-created by Rails.

When I first created the controller, I typed rails generate controller Say hello goodbye and the views for hello and goodbye were simultaneously created. If I run that command again with a new method name, I get a conflict error:

So I’ll add them via the text editor and then I’ll be sure to add the corresponding views.

Answer to Question 2: Because it’s erb! That’s gotta go in the html.erb file, kid.

Next post, here.