Understanding Rails

Understanding Rails · On the job

Ticket 003: Long notes get rejected

A ticket as it arrived in Switchback, a small Rails app. Before you fix anything, make it ready to work: ask the reporter what you need to know, write what done looks like, then compare with how a senior refined it.

Helps to know first

1. The ticket, as it arrived

From: Morgan, the product manager

Since we fixed the vanishing notes, hikers at least find out when a note is too long. But now support hears from people who wrote a whole report and only found out when they pressed Post. Can we stop people writing essays that get rejected? Could we just raise the limit to 5,000?

I wrote up the washout on Granite Switchbacks on my phone at the trailhead, every crossing. I pressed Post and it said my note was too long. Too long by how much? I had to guess what to cut, standing in the rain.

Dana, a regular hiker

2. Ask the reporter

Morgan sent it in. Morgan is the product manager, not an engineer, and is asking for a change, with a fix in mind. Ask what you need to know; each question shows the answer. Some answers narrow the cause, and some don't.

  • Why is the limit 500?

    Morgan: Notes are for trail conditions. People read them on a phone at the trailhead, and every new note goes out by email to the trail's followers. When notes ran long, the short useful ones got buried.

  • What are hikers writing on?

    Morgan: Mostly phones, at the trailhead or in the car after. Some write at home on a laptop.

  • Would raising the limit to 5,000 fix it?

    Morgan: For a while, maybe. The people who write long would write longer and hit 5,000 instead. And followers already tell us the emails are long. I suggested it because it sounded quick. If hikers could see the limit coming, I'd keep 500.

  • How many hikers have hit this?

    Morgan: Support heard from six this month. That's a lot for us. Most people never write in.

  • When does a hiker find out about the limit today?

    Morgan: Only after they press Post. Nothing on the form says there's a limit. I looked, and I couldn't find it either.

  • Should the form stop them typing at 500?

    Morgan: I'd hate that, getting cut off mid-sentence. Dana writes her reports in her notes app and pastes them in. I'd rather she sees she's over by 40 and trims it herself.

  • Can hikers edit a note after they post it?

    Morgan: No, not yet. They post a new one.

  • What counts as a character? Do hikers use emoji or line breaks?

    Morgan: All the time: boots, mountains, the warning sign. And long reports get broken into lines. I don't mind how it counts, as long as the number they see is the number that gets checked.

  • Do long notes cost us anything to store?

    Morgan: Not that anyone's mentioned. It's just text.

  • Does it need to work on a bad connection?

    Morgan: Yes. Some trailheads get one bar. Sometimes the page only half loads, and people still post from it.

  • Do any hikers use a screen reader?

    Morgan: Yes. Jo leads our accessible-trails group and posts from her phone with a screen reader. Whatever we add, she should hear it too. She says some sites read a number out after every letter, and she turns those off.

Write the ticket

What must be true when this is done? And what will this ticket leave alone? It stays in your browser.

3. Compare

Compare with the refined ticket

The refined ticket

The note form doesn't show the 500-character limit until it's too late

Story: As a hiker writing a long trail report on my phone, I want to see how much room I have left as I type, so I can trim my note before I post it, not after.

What happens today: Sign in, open any trail, and look at the note form. Nothing says there's a limit. Type or paste 600 characters and post. With ticket 001 fixed, the page says the note is too long, but not by how much. Either way, the hiker finds out only after writing the whole note.

Why it happens: The 500-character limit lives only in Note's validation, which runs on the server after the hiker posts. The form never mentions it.

What the refined ticket says

Acceptance criteria

  1. Before typing, the form says notes can be up to 500 characters, even without JavaScript.
  2. As the hiker types, the form shows how many characters are left, counted the way the server counts.
  3. The hiker can type past the limit, nothing is cut off, and the form shows how far over it is.
  4. A screen reader politely hears how far over the limit the note is.
  5. The server still rejects a note over 500 characters, and a note of 500 still posts.

Criterion 2 is about agreeing with the server, not one way to count. Rails' length validation counts code points, so 🥾 is one character and 👨‍👩‍👧 is five. A browser's value.length counts UTF-16 units, so 🥾 is two. With Turbo, each line break reaches the server as one character. "Politely" means aria-live="polite" or role="status": the screen reader waits until the hiker stops. The count can show as left or as used.

Out of scope

  • Raising the limit. Morgan would rather keep 500 if hikers can see it coming.
  • Counting what looks like one character, such as 👨‍👩‍👧, as one. The server would have to count that way too.
  • Line breaks in a note sent without JavaScript. The browser sends each one as two characters, so the server counts more than Turbo sends. A small next ticket if hikers hit it.
  • Ticket 001's error message. If your master doesn't have it yet, do ticket 001 first.
  • Editing a note after it's posted.

What a senior asked first, and why

  1. Would raising the limit to 5,000 fix it? Before building the fix a reporter suggests, ask whether it fixes the problem. It doesn't: hikers would hit 5,000 instead. The limit isn't the problem. Finding out too late is.
  2. When does a hiker find out about the limit today? That turns the ticket from changing the limit into showing it. The hiker needs to see the limit, and how close they are, before they press Post.
  3. What counts as a character? Do hikers use emoji or line breaks? "The number they see is the number that gets checked" is a testable rule. Rails counts code points, so a boot emoji is one character. A browser's value.length counts it as two. A counter that disagrees with the server tells the hiker they have room when they don't.
  4. Should the form stop them typing at 500? A counter that stops typing at 500, or a maxlength on the box, cuts off a pasted report with no warning: words lost, as in ticket 001. Let the hiker go over and show by how much. The server still decides.
  5. Does it need to work on a bad connection? A half-loaded page may not run JavaScript yet. So the limit has to be in the page's HTML, and the server keeps checking it.
  6. Do any hikers use a screen reader? A counter that changes as you type has to be heard too, but not after every letter. A polite live region waits until the hiker pauses.

Why 500, the device, how many hikers, editing and storage are fair questions. Morgan answered each one honestly. Why 500 explains the limit, and phones mean the counter has to fit a narrow screen, but neither changes what to build. After the first questions, a senior types an emoji into a test and compares what the browser and Rails count.

Work it

Now fix it, in your own copy of Switchback. The branch is 003-long-notes-rejected.

  1. Do ticket 001 first: this ticket builds on its fix. Use your fork of Switchback, and bring it up to date: on GitHub, click Sync fork, then pull.
  2. Make a branch named for the ticket, from your fork's master with your 001 fix in it. The review finds the ticket by the number the branch starts with.
    git switch -c 003-long-notes-rejected
  3. Write a test that fails, then build it, and commit with a message that says why. The counter runs in the browser, so a test for it needs one: a system test with headless Chrome. An ordinary integration test can check the limit in the page and the server's answer.
    bin/rails generate system_test note_form
  4. Push the branch and open a pull request inside your fork, from 003-long-notes-rejected into your fork's master. Change the base repository to your fork, as in ticket 001, or the review can't comment.
    git push -u origin 003-long-notes-rejected
  5. A GitHub Action runs the ticket's acceptance tests on your pull request, some of them in a real browser, and comments with what passes, what fails and what crashed. Once all five pass, it lists the questions a reviewer would still ask. Push again and the same comment updates.

4. Watch a senior work it

Once you've tried it, see how a senior refines and fixes the same ticket, dead ends and all.

Watch episode 103