<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:media="http://search.yahoo.com/mrss/"><channel><title><![CDATA[ENTR-451]]></title><description><![CDATA[ENTR-451: Introduction to Software Development, Kellogg School of Management]]></description><link>https://entr451.com/</link><image><url>https://entr451.com/favicon.png</url><title>ENTR-451</title><link>https://entr451.com/</link></image><generator>Ghost 4.20</generator><lastBuildDate>Tue, 01 Dec 2026 02:21:53 GMT</lastBuildDate><atom:link href="https://entr451.com/rss/" rel="self" type="application/rss+xml"/><ttl>60</ttl><item><title><![CDATA[Technical Debt]]></title><description><![CDATA[<!--kg-card-begin: markdown--><p>If you haven&apos;t already, first read the <a href="https://entr451.com/encryption-and-data-security/">previous post</a> about Encryption &amp; Data Security. This one picks up where it left off: with the user Ben, locked out.</p>
<h2 id="legacy-data">Legacy data</h2>
<p>When we started hashing passwords, we changed the rules for how a password is stored. Every user who</p>]]></description><link>https://entr451.com/technical-debt/</link><guid isPermaLink="false">6af53cdbd9019e19441c36a8</guid><dc:creator><![CDATA[Ben Block]]></dc:creator><pubDate>Thu, 12 Nov 2026 04:15:41 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><p>If you haven&apos;t already, first read the <a href="https://entr451.com/encryption-and-data-security/">previous post</a> about Encryption &amp; Data Security. This one picks up where it left off: with the user Ben, locked out.</p>
<h2 id="legacy-data">Legacy data</h2>
<p>When we started hashing passwords, we changed the rules for how a password is stored. Every user who signs up from now on gets a bcrypt hash. But Ben and Brian signed up before the change. Their rows are still exactly the way the old code left them: the password as typed.</p>
<p>That&apos;s <strong>legacy data</strong>: rows created under rules the code no longer follows. Nothing is wrong with the rows themselves. They were correct when they were written. The code moved on, and they didn&apos;t move with it.</p>
<p>Every app that lives long enough has it. A <code>user_id</code> column added to posts after a thousand posts existed: those posts belong to nobody. A required phone number added after half your customers signed up without one. A price that used to be in dollars and is now in cents. Any change to the shape of your data has to answer one question: <em>what happens to the rows that are already there?</em></p>
<p>We didn&apos;t answer it. We made a good change - storing passwords safely - and the result was two customers who can&apos;t log in.</p>
<h2 id="technical-debt">Technical debt</h2>
<p>Say we don&apos;t have time to fix it properly today. What would we do in the meantime? We could check the password both ways: if the stored value looks like a bcrypt hash, check it with bcrypt; otherwise, compare it as typed. Or we could check when the user signed up, and send everyone from before a certain date down an old path while everyone else takes the new one.</p>
<p>Both work. Both are extra code whose only job is to accommodate the change. That&apos;s <strong>technical debt</strong>: code (or data) that exists because of a shortcut, or a change we haven&apos;t finished dealing with, rather than because the product needs it.</p>
<p>The name is accurate. Like money you borrow, it buys you something now - the release goes out, Ben can log in today - and you pay interest on it for as long as you carry it. The interest is everything that gets harder:</p>
<ul>
<li>Every engineer who touches the login now has to understand both paths, and why there are two.</li>
<li>Every new feature that touches passwords (a reset link, two-factor, a rule against &quot;password&quot; as a password) has to be built twice, or decide to ignore the old users.</li>
<li>Every path is a place for a bug, and the old path is the one nobody tests anymore.</li>
</ul>
<p>And the interest compounds. Workarounds get built on top of workarounds. The longer debt lives, the more code depends on it, until it isn&apos;t a side path anymore but part of the foundation, and paying it off means a whole team stopping to plan how to change something everything else stands on.</p>
<h2 id="paying-it-off">Paying it off</h2>
<p>Debt has to be paid off at some point: someone has to go back and make the change complete, so the extra code can go. For Ben and Brian, that could be:</p>
<ul>
<li><strong>Migrate them.</strong> The plain passwords are still in the table, so a one-time script can hash every old row and the workaround can be deleted. Nobody notices.</li>
<li><strong>Force a reset.</strong> Email everyone from before the change a link to choose a new password.</li>
<li><strong>Upgrade on login.</strong> An app moving off an old, weak hash can&apos;t run that script, because it doesn&apos;t have the passwords either. It checks the old way one last time when each user logs in, then saves the new hash. The workaround stays until the last old user has logged in, or until you decide they never will.</li>
</ul>
<p>None of these is only an engineering choice. An engineer can tell you what each one costs and what can go wrong. But &quot;do we email 10,000 customers and ask them to reset their password?&quot; is a product question: what will customers think, who writes the email, what happens to the ones who never click. &quot;Do we take the login offline for an hour to migrate?&quot; is a product question too. So is the choice of what to do <em>until</em> the debt is paid - keep the workaround, lock the old users out, or tell them. <strong>Technical debt is a product decision and an engineering decision at the same time</strong>, and it goes best when the people making each half talk to each other.</p>
<h2 id="making-time-for-it">Making time for it</h2>
<p>Paying off debt doesn&apos;t move the product forward. No customer sees it, and the return is hard to point to, so it&apos;s always the easiest work to push to next sprint. That&apos;s how six months of &quot;next sprint&quot; happens.</p>
<p>What works better is small and regular:</p>
<ul>
<li><strong>Give it time on a schedule.</strong> Every three or four weeks, give an engineer a day or half a day to work on whatever debt is causing them the most pain.</li>
<li><strong>Take turns.</strong> One engineer one cycle, another the next, so two people aren&apos;t rewriting the same foundation at once on branches that will conflict.</li>
<li><strong>Ask the engineers.</strong> You don&apos;t need a search. Anyone who has worked in a codebase knows where the debt lives: the code they tiptoe around, the file they hope they won&apos;t have to open. They may already have told you.</li>
<li><strong>Start with the code you touch most.</strong> The login, the payment form, whatever every request goes through. Debt in a corner nobody visits costs little. Debt on the busiest path costs every day.</li>
</ul>
<p>If you&apos;re building on your own with an agent, the same habit applies: every week or so, ask your agent to look through the codebase for technical debt it can see - duplicated code, workarounds, paths that only exist for old data - and pick one thing to clean up. An agent adds code quickly, which means it adds debt quickly too.</p>
<h2 id="practice">Practice</h2>
<p>If you want to pay this one off yourself, the Practice section at the end of the <code>auth</code> README (<a href="https://github.com/dsgn425-fall2026/auth#practice">github.com/dsgn425-fall2026/auth</a>) has it as an exercise: move Ben and Brian onto hashed passwords with a one-time script. You write the user story and the definition of done first. Deciding what &quot;paid off&quot; means is the hard part.</p>
<p>Next: how a user who has logged in <em>stays</em> logged in, with <a href="https://entr451.com/cookies/">cookies</a>.</p>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[Practice - Auth]]></title><description><![CDATA[<!--kg-card-begin: markdown--><h2 id="practice">Practice</h2>
<p><strong>Additional exercises for the <code>auth</code> repository:</strong> <a href="https://github.com/dsgn425-fall2026/auth">https://github.com/dsgn425-fall2026/auth</a></p>
<p>For each of the following: make a branch, open an issue for it (<strong>Issues</strong>, <strong>New issue</strong>, paste the story and the definition of done), build it by hand or with your agent, one test per sentence, and open</p>]]></description><link>https://entr451.com/practice-auth/</link><guid isPermaLink="false">6af5326ed9019e19441c366d</guid><dc:creator><![CDATA[Ben Block]]></dc:creator><pubDate>Thu, 12 Nov 2026 03:38:24 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><h2 id="practice">Practice</h2>
<p><strong>Additional exercises for the <code>auth</code> repository:</strong> <a href="https://github.com/dsgn425-fall2026/auth">https://github.com/dsgn425-fall2026/auth</a></p>
<p>For each of the following: make a branch, open an issue for it (<strong>Issues</strong>, <strong>New issue</strong>, paste the story and the definition of done), build it by hand or with your agent, one test per sentence, and open a pull request that closes the issue.</p>
<h3 id="1-one-account-per-email">1. One account per email</h3>
<p>Sign up with <code>ben@example.com</code>. It works: there are now two users with the same email, and logging in finds whichever one the database returns first.</p>
<blockquote>
<p><strong>One account per email</strong></p>
<p>As a visitor, I can&apos;t sign up with an email that already has an account, so every email belongs to exactly one person.</p>
<p>Definition of done</p>
<ul>
<li>Signing up with an email that already has an account does not create a second user.</li>
<li>Instead, it sends me back to the sign-up form with a message that the email already has an account.</li>
<li>Signing up with an email nobody has used still creates the user and logs them in.</li>
</ul>
</blockquote>
<p>Once it works, think about this: the login form gives one message, &quot;Nope.&quot;, for a wrong password and for an email with no account, so nobody can use it to find out who has an account. Your sign-up form now tells anyone who asks. Is that a problem? What do real apps do?</p>
<h3 id="2-legacy-datatechnical-debt">2. Legacy data - technical debt</h3>
<p>Once password encryption has been implemented, Ben and Brian can no longer log in. Their rows were saved before it: their passwords are still in the plain <code>password</code> column, and <code>password_digest</code> is empty, so login has nothing to check them against. One way out is a one-time script that moves every old row onto a hashed password. That&apos;s this exercise.</p>
<p>This one has no ticket. <strong>Writing it is the exercise.</strong> Before you build anything, write the issue yourself:</p>
<ul>
<li><strong>The story.</strong> Who is it for, what do they want, and why? (Is the user here Ben? You? The company?)</li>
<li><strong>The definition of done.</strong> Three to five sentences, each one something a test can check. Ask yourself:
<ul>
<li>What must be true for Ben and Brian once the script has run?</li>
<li>What must be true about the <code>password</code> column?</li>
<li>What must stay true for the users who signed up after the change?</li>
<li>What happens if someone runs the script twice?</li>
<li>Does a wrong password still fail?</li>
</ul>
</li>
</ul>
<p>Then build it: a script in <code>scripts/</code>, run with <code>bin/rails runner</code>, the same way you run <code>create_data.rb</code>.</p>
<p>Two things to know:</p>
<ul>
<li><code>scripts/create_data.rb</code> puts Ben and Brian back the old way every time it runs, on purpose. Run your script again after it.</li>
<li>Before you call it done, run <code>bin/rails runner scripts/show_data.rb User</code> and look.</li>
</ul>
<h3 id="3-edit-your-own-post">3. Edit your own post</h3>
<p>Posts can be created and deleted, but a typo is forever. Add editing, with the same rule as delete: only your own.</p>
<blockquote>
<p><strong>Edit my own post</strong></p>
<p>As a logged-in user, I can fix a post I made, and nobody else can change it.</p>
<p>Definition of done</p>
<ul>
<li>On the feed, each post that belongs to me has an Edit link to /posts/:id/edit.</li>
<li>The edit page is a form filled in with the post&apos;s body and image.</li>
<li>Saving it changes the post and sends me back to the feed, where the new text shows.</li>
<li>Signed in as a different user, opening the edit page for a post that is not mine sends me back to the feed.</li>
<li>Signed in as a different user, a PATCH request to /posts/:id for a post that is not mine changes nothing.</li>
<li>Not signed in at all, the edit page and the PATCH request both send me to the login page, and nothing changes.</li>
</ul>
</blockquote>
<p>Companies already have an edit page and an <code>update</code> action; they&apos;re the pattern. The last three sentences are the abuse story, folded into the same ticket. Notice there are two actions to protect, not one: the form and the request that saves it.</p>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[Auth - The four Tacostagram tickets - Solution]]></title><description><![CDATA[<!--kg-card-begin: markdown--><h2 id="where-this-starts">Where this starts</h2>
<p>Your copy of <code>auth</code> came with four issues in its Issues tab, made by <code>bin/ticket</code> when the Codespace was built. In class we ran the first three with an agent (&quot;Build GitHub issue number ...&quot;), one at a time, and talked through the fourth. This</p>]]></description><link>https://entr451.com/auth-posts-lab-solution/</link><guid isPermaLink="false">6af5256cd9019e19441c3647</guid><dc:creator><![CDATA[Ben Block]]></dc:creator><pubDate>Thu, 12 Nov 2026 02:35:54 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><h2 id="where-this-starts">Where this starts</h2>
<p>Your copy of <code>auth</code> came with four issues in its Issues tab, made by <code>bin/ticket</code> when the Codespace was built. In class we ran the first three with an agent (&quot;Build GitHub issue number ...&quot;), one at a time, and talked through the fourth. This post is all four, built the same way: one prompt per ticket, one test per sentence of each definition of done.</p>
<p>If you haven&apos;t run them yourself, do that first. Point your agent at the first issue, read the tests it writes against the sentences, run <code>bin/rails test</code>, and only then come back here. Yours will not match this line for line, and should not. What should match is where each rule lives.</p>
<p>The starting point is the Tacostagram from the end of web-apps-2: anyone can open <code>/posts/new</code>, the form has an author box, and whatever name you type is the name on the post. The app now has users and a login, but the posts don&apos;t know about either.</p>
<h2 id="ticket-1-only-members-can-post">Ticket 1: Only members can post</h2>
<blockquote>
<p>As an anonymous visitor, I can read the feed but not post to it, so Tacostagram stays a place where every post has a person behind it.</p>
<ul>
<li>The feed at /posts still shows every post to a visitor who is not logged in.</li>
<li>A visitor who is not logged in and opens the new post page is sent to the login page instead.</li>
<li>A visitor who is not logged in and submits the form does not create a post, and is sent to the login page.</li>
</ul>
</blockquote>
<p><code>@current_user</code> is already set before every action, by <code>find_current_user</code> in the application controller. The agent added a second method beside it, so any controller can ask for a login:</p>
<pre><code class="language-ruby">class ApplicationController &lt; ActionController::Base
  # Runs before every action in every controller.
  before_action :find_current_user

  def find_current_user
    # the session is a small, signed box that travels with the browser as a cookie;
    # logging in puts the user&apos;s id in it, logging out empties it
    @current_user = User.find_by({ &quot;id&quot; =&gt; session[&quot;user_id&quot;] })
  end

  # Use as a before_action on actions that only members may reach.
  # Rails runs it after find_current_user, so @current_user is already set.
  def require_login
    if @current_user.nil?
      # nobody is logged in: send the visitor to the login form instead
      flash[&quot;notice&quot;] = &quot;You must be logged in to do that.&quot;
      redirect_to(&quot;/login&quot;)
    end
  end
end
</code></pre>
<p>And one line at the top of <code>PostsController</code>:</p>
<pre><code class="language-ruby">before_action(:require_login, { :only =&gt; [&quot;new&quot;, &quot;create&quot;] })
</code></pre>
<p><code>index</code> is not on the list, so the feed stays public. Notice the third sentence: it is not enough to hide the form. Someone can send a POST to <code>/posts</code> without ever opening the page (the tests do exactly that), so <code>create</code> has to be guarded too.</p>
<h2 id="ticket-2-posts-carry-their-author">Ticket 2: Posts carry their author</h2>
<blockquote>
<p>As a signed-in user, my posts show my name, so nobody has to type a name and nobody can type someone else&apos;s.</p>
<ul>
<li>The new post form has fields for the body and the image, and no field for the author.</li>
<li>Submitting the form creates a post whose user_id is the id of the logged-in user, taken from the session and never from the form.</li>
<li>On the feed, a post shows the first name of the user it belongs to.</li>
</ul>
</blockquote>
<p>The author box comes out of <code>app/views/posts/new.html.erb</code>, leaving body and image. In <code>create</code>, the author is no longer read from <code>params</code>:</p>
<pre><code class="language-ruby">def create
  # start with a new Post
  post = Post.new
  # assign the form&apos;s values to the Post&apos;s columns
  post[&quot;body&quot;] = params[&quot;body&quot;]
  post[&quot;image&quot;] = params[&quot;image&quot;]
  # the author is whoever is logged in, read from the session (via @current_user),
  # never from the form, so nobody can post as someone else
  post[&quot;user_id&quot;] = @current_user[&quot;id&quot;]
  # save the Post row
  post.save
  # redirect the user
  redirect_to(&quot;/posts&quot;)
end
</code></pre>
<p>This is the lesson of the ticket. A hidden <code>user_id</code> field in the form would <em>look</em> the same to the user, and it would let anyone post as anyone: open the inspector, change the number, submit. Everything the browser sends is the user&apos;s to change. The session is the one thing they can&apos;t edit, because Rails signs it.</p>
<p>On the feed, each post looks up its user:</p>
<pre><code class="language-erb">&lt;% user = User.find_by({ &quot;id&quot; =&gt; post[&quot;user_id&quot;] }) %&gt;
&lt;% if user %&gt;
  &lt;h3&gt;&lt;%= user[&quot;first_name&quot;] %&gt;&lt;/h3&gt;
&lt;% else %&gt;
  &lt;%# posts from before user_id existed have no user; they still carry an author %&gt;
  &lt;h3&gt;&lt;%= post[&quot;author&quot;] %&gt;&lt;/h3&gt;
&lt;% end %&gt;
</code></pre>
<p>The <code>else</code> is the agent&apos;s judgment call, and it said so when it finished: &quot;the ticket does not say what the feed should show for the two legacy posts that have an author but no user_id. I kept showing their author rather than a blank heading.&quot; The two posts from before the <code>user_id</code> column have no user. The ticket didn&apos;t say what to do with them, and the agent flagged it instead of guessing quietly. That is the same legacy-data question as the two users whose passwords were still plain text: every change to the shape of the data has to decide what happens to the rows that are already there.</p>
<h2 id="ticket-3-delete-my-own-post">Ticket 3: Delete my own post</h2>
<blockquote>
<p>As a signed-in user, I want to delete a post I made, so a taco I regret posting is gone from the feed.</p>
<ul>
<li>On the feed, each post that belongs to the logged-in user has a Delete button.</li>
<li>Pressing it deletes that post and sends the user back to the feed, where the post is gone.</li>
<li>The button sends a DELETE request to /posts/:id, following the pattern used for companies.</li>
</ul>
</blockquote>
<p>The button is the companies pattern: a form that posts to the post&apos;s address, with a hidden <code>_method</code> of <code>delete</code>. It only appears on your own posts:</p>
<pre><code class="language-erb">&lt;% if @current_user &amp;&amp; post[&quot;user_id&quot;] == @current_user[&quot;id&quot;] %&gt;
  &lt;form action=&quot;/posts/&lt;%= post[&quot;id&quot;] %&gt;&quot; method=&quot;post&quot;&gt;
    &lt;input type=&quot;hidden&quot; name=&quot;_method&quot; value=&quot;delete&quot;&gt;
    &lt;button&gt;Delete&lt;/button&gt;
  &lt;/form&gt;
&lt;% end %&gt;
</code></pre>
<p>And the action:</p>
<pre><code class="language-ruby">def destroy
  # find the Post whose id is in the address, but only among the logged-in
  # user&apos;s own posts, so nobody can delete someone else&apos;s by guessing an id
  post = Post.find_by({ &quot;id&quot; =&gt; params[&quot;id&quot;], &quot;user_id&quot; =&gt; @current_user[&quot;id&quot;] })
  # destroy the Post row, if it is theirs
  if post
    post.destroy
  end
  # redirect the user back to the feed
  redirect_to(&quot;/posts&quot;)
end
</code></pre>
<p><code>destroy</code> also joins the login list, so the line at the top becomes:</p>
<pre><code class="language-ruby">before_action(:require_login, { :only =&gt; [&quot;new&quot;, &quot;create&quot;, &quot;destroy&quot;] })
</code></pre>
<p>Look at the <code>find_by</code>. It does not find the post and then check whose it is; it only ever looks among your own posts. Someone else&apos;s post is never found, so it can never be destroyed. The ticket didn&apos;t ask for that. The agent did it anyway, which brings us to ticket 4.</p>
<h2 id="ticket-4-nobody-deletes-your-post">Ticket 4: Nobody deletes your post</h2>
<blockquote>
<p>As another user, I want to delete your post. This must fail: a post can only be deleted by the user it belongs to.</p>
<ul>
<li>Signed in as a different user, the feed shows no Delete button on a post that is not mine.</li>
<li>Signed in as a different user, a DELETE request to /posts/:id for a post that is not mine is refused: the post is still there afterwards.</li>
<li>Not signed in at all, a DELETE request to /posts/:id is refused the same way.</li>
</ul>
</blockquote>
<p>This is an <strong>abuse story</strong>: a user story for something that must never work. It reads like the others, but you&apos;re writing it from the attacker&apos;s chair. You build against it, not for it.</p>
<p>When the agent got to it, it changed no app code at all. Its words: &quot;The protection already lives in the controller from ticket 3.&quot; Ticket 4&apos;s whole contribution is three tests. That is the point, not a letdown. Before ticket 4, the rule &quot;nobody deletes your post&quot; was true by accident, a side effect of how one line in <code>destroy</code> happened to be written. After ticket 4, it is true on purpose, and a test fails the day someone rewrites <code>destroy</code> and loses it.</p>
<p>The middle sentence is the one that matters. A hidden button is not a rule: anyone can send a DELETE without the button, the same way the test does. The rule lives in the controller, because every request arrives there, whether or not it came from your page.</p>
<h2 id="the-tests">The tests</h2>
<p>Twelve tests, one per sentence, named with the sentence, in <code>test/integration/posts_page_test.rb</code>. Three helpers at the bottom build users and posts the way the app does, and log in through <code>/login</code> like a person would. Here are the two that carry the most weight. Ticket 2&apos;s second sentence smuggles someone else&apos;s id into the form and checks that it is ignored:</p>
<pre><code class="language-ruby">test &quot;Submitting the form creates a post whose user_id is the id of the logged-in user, taken from the session and never from the form.&quot; do
  pat = sign_up_and_log_in(&quot;Pat&quot;)
  sam = create_user(&quot;Sam&quot;)

  # the form smuggles in someone else&apos;s id; it must be ignored
  post(&quot;/posts&quot;, :params =&gt; { &quot;body&quot; =&gt; &quot;Carnitas for days&quot;, &quot;image&quot; =&gt; &quot;&quot;, &quot;user_id&quot; =&gt; sam[&quot;id&quot;] })

  created_post = Post.find_by({ &quot;body&quot; =&gt; &quot;Carnitas for days&quot; })
  assert_equal(pat[&quot;id&quot;], created_post[&quot;user_id&quot;])
  assert_not_equal(sam[&quot;id&quot;], created_post[&quot;user_id&quot;])
end
</code></pre>
<p>Ticket 4&apos;s second sentence sends the DELETE with no button involved:</p>
<pre><code class="language-ruby">test &quot;Signed in as a different user, a DELETE request to /posts/:id for a post that is not mine is refused: the post is still there afterwards.&quot; do
  sign_up_and_log_in(&quot;Pat&quot;)
  sam = create_user(&quot;Sam&quot;)
  sams_post = create_post(sam, &quot;Al pastor forever&quot;)

  delete(&quot;/posts/#{sams_post[&quot;id&quot;]}&quot;)

  assert_redirected_to(&quot;/posts&quot;)
  assert_not_nil(Post.find_by({ &quot;id&quot; =&gt; sams_post[&quot;id&quot;] }))
end
</code></pre>
<p>And the helpers:</p>
<pre><code class="language-ruby">private

# Builds a Post the way posts_controller#create does, owned by the given user.
def create_post(user, body)
  taco_post = Post.new
  taco_post[&quot;body&quot;] = body
  taco_post[&quot;user_id&quot;] = user[&quot;id&quot;]
  taco_post.save
  taco_post
end

# Builds a User the way users_controller#create does, with a known password.
def create_user(first_name)
  user = User.new
  user[&quot;first_name&quot;] = first_name
  user[&quot;last_name&quot;] = &quot;Tester&quot;
  user[&quot;email&quot;] = &quot;#{first_name.downcase}@example.com&quot;
  user.password = &quot;password&quot;
  user.save
  user
end

# Creates a User and logs them in through /login, as sessions_controller expects.
def sign_up_and_log_in(first_name)
  user = create_user(first_name)
  post(&quot;/login&quot;, :params =&gt; { &quot;email&quot; =&gt; user[&quot;email&quot;], &quot;password&quot; =&gt; &quot;password&quot; })
  user
end
</code></pre>
<pre><code>bin/rails test
12 runs, 49 assertions, 0 failures, 0 errors, 0 skips
</code></pre>
<p>Read your agent&apos;s tests the way you&apos;d read these: each one against its sentence. Does the test for &quot;is refused&quot; actually send the request, or does it only check that the button is missing? The second kind passes even when the controller lets anyone delete anything.</p>
<h2 id="what-to-take-from-it">What to take from it</h2>
<ul>
<li><strong>Authentication</strong> (who are you?) is the session: <code>@current_user</code>, set before every action. <strong>Authorization</strong> (what may you do?) is every decision made with it: <code>require_login</code>, the <code>user_id</code> from the session, the <code>find_by</code> scoped to your own posts.</li>
<li><strong>Never trust the browser.</strong> Hidden fields, hidden buttons and the form itself are the user&apos;s to change. The rule goes in the controller.</li>
<li><strong>Write the abuse stories down.</strong> They turn a rule that is true by accident into one that is tested on purpose, and an agent will build and test against them like any other ticket.</li>
</ul>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[Authorization]]></title><description><![CDATA[<!--kg-card-begin: markdown--><p>If you haven&apos;t already, first read the <a href="https://entr451.com/cookies/">previous post</a> about Cookies.</p>
<p>Authorization refers to what a user can do once they&apos;re authenticated. It also includes what a user sees (and what a visitor doesn&apos;t see). We&apos;ve already seen the simplest version: the</p>]]></description><link>https://entr451.com/authorization/</link><guid isPermaLink="false">6af524d2d9019e19441c3632</guid><dc:creator><![CDATA[Ben Block]]></dc:creator><pubDate>Thu, 12 Nov 2026 02:32:29 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><p>If you haven&apos;t already, first read the <a href="https://entr451.com/cookies/">previous post</a> about Cookies.</p>
<p>Authorization refers to what a user can do once they&apos;re authenticated. It also includes what a user sees (and what a visitor doesn&apos;t see). We&apos;ve already seen the simplest version: the navbar checks <code>if @current_user</code> and shows your name and <strong>Log out</strong>, or <strong>Sign up</strong> and <strong>Log in</strong>.</p>
<p>That&apos;s what the app <em>shows</em>. Let&apos;s look at what it <em>allows</em>.</p>
<h2 id="someone-elses-account">Someone else&apos;s account</h2>
<p>Log in, and click your name in the navbar. That&apos;s your account page, <code>/users/3</code> (or whatever your id is), with your name and your email.</p>
<p>Now change the number in the address bar to <code>/users/2</code>.</p>
<p>That&apos;s Brian&apos;s account page, and his email. You&apos;re not Brian. Try <code>/users/1</code>. Try them logged out. Anyone who can count can read every user&apos;s email in our app.</p>
<p>Here&apos;s the <code>show</code> action in <code>app/controllers/users_controller.rb</code>:</p>
<pre><code class="language-ruby">def show
  # find the User whose id is in the address
  @user = User.find_by({ &quot;id&quot; =&gt; params[&quot;id&quot;] })
  # render users/show view
  render({ :template =&gt; &quot;users/show&quot; })
end
</code></pre>
<p>It finds whichever user is in the address and shows them, no questions asked. Nothing is wrong with the code; nobody wrote down the rule. This hole has a name: <strong>broken access control</strong>, and it&apos;s number one on the <a href="https://owasp.org/Top10/">OWASP Top 10</a>, the industry&apos;s list of the most common serious flaws in web applications. Not because it&apos;s clever. Because it&apos;s easy to forget.</p>
<p>The rule is: only the user themself gets to see this page. Now that we have <code>@current_user</code>, that&apos;s one <code>if</code>:</p>
<pre><code class="language-ruby">def show
  # find the User whose id is in the address
  @user = User.find_by({ &quot;id&quot; =&gt; params[&quot;id&quot;] })
  # only the user themself gets to see this page: the user in the address has to be
  # the user who is logged in, or the request is turned away
  if @current_user.nil? || @user != @current_user
    flash[&quot;notice&quot;] = &quot;That isn&apos;t your account.&quot;
    redirect_to(&quot;/posts&quot;)
    return
  end
  # render users/show view
  render({ :template =&gt; &quot;users/show&quot; })
end
</code></pre>
<p>If nobody is logged in, or the user in the address isn&apos;t the user who&apos;s logged in, we send them away with a message. The <code>return</code> stops the action there, so the page never renders. Try <code>/users/2</code> again: &quot;That isn&apos;t your account.&quot; Your own page still works.</p>
<p>Notice where the rule lives: in the controller, not the view. Every request arrives at the controller, whether it came from a link on our page or from someone typing an address. Hiding a link is a courtesy. The controller is the lock.</p>
<p>If you fell behind, <code>code-along-complete/3-access/</code> in your copy has this file finished.</p>
<h2 id="tacostagram">Tacostagram</h2>
<p>Now the posts. Right now, anyone can post a taco under any name they type, and posts don&apos;t belong to anyone. Here&apos;s who should be able to do what:</p>
<ul>
<li>A visitor can read the feed, but can&apos;t post to it.</li>
<li>A logged-in user&apos;s posts carry their name, and nobody can post as someone else.</li>
<li>A logged-in user can delete their own post.</li>
<li>And nobody can delete <em>your</em> post.</li>
</ul>
<p>These are the four tickets in your copy&apos;s <strong>Issues</strong> tab, each with a definition of done. Build them first: point your agent at each issue, one at a time, and read the tests it writes against the sentences. Then compare with the full walkthrough, <a href="https://entr451.com/auth-posts-lab-solution/">The four Tacostagram tickets</a>.</p>
<p>Without giving the whole thing away, here are the rules as they end up in the code.</p>
<p><strong>Only members.</strong> A second method in the application controller, used as a <code>before_action</code> on the actions that need a login:</p>
<pre><code class="language-ruby">before_action(:require_login, { :only =&gt; [&quot;new&quot;, &quot;create&quot;, &quot;destroy&quot;] })
</code></pre>
<p><code>index</code> isn&apos;t on the list, so the feed stays public. And <code>create</code> is on it, not just <code>new</code>: hiding the form doesn&apos;t stop someone from sending a POST to <code>/posts</code> without it.</p>
<p><strong>The author comes from the session, never the form.</strong> We could put a hidden <code>user_id</code> field in the post form with the current user&apos;s id. But anything in the browser is the user&apos;s to change: open the developer tools, change the number, submit, and you&apos;ve posted as someone else. Instead, the backend assigns it, in code only we can touch:</p>
<pre><code class="language-ruby">post[&quot;user_id&quot;] = @current_user[&quot;id&quot;]
</code></pre>
<p><strong>Your posts, and only yours.</strong> To delete a post, don&apos;t find it and then check whose it is. Only ever look among the current user&apos;s posts:</p>
<pre><code class="language-ruby">post = Post.find_by({ &quot;id&quot; =&gt; params[&quot;id&quot;], &quot;user_id&quot; =&gt; @current_user[&quot;id&quot;] })
</code></pre>
<p>Someone else&apos;s post is never found, so it can never be destroyed, even by a DELETE request that didn&apos;t come from our page.</p>
<h2 id="abuse-stories">Abuse stories</h2>
<p>The fourth ticket is a different kind of user story: &quot;As another user, I want to delete your post.&quot; It&apos;s written from the attacker&apos;s chair, and it describes something that must <em>never</em> work. These are sometimes called abuse stories.</p>
<p>Write them down next to the user stories. What would a malicious user try in your app? Each one becomes a ticket with a definition of done, and each sentence gets a test that proves the refusal. Then the rule isn&apos;t true by accident, a side effect of how one line happens to be written. It&apos;s true on purpose, and the day someone (or some agent) rewrites that line and loses it, a test fails.</p>
<h2 id="just-conditionals">Just conditionals</h2>
<p>Besides the concept of <code>@current_user</code> and how it gets assigned in a <code>before_action</code>, you may have noticed that the rest of this authorization code is no different from anything we&apos;ve done before. Authorization at its core is conditional logic, SQL queries and column assignment:</p>
<ul>
<li><code>if @current_user</code></li>
<li><code>@user != @current_user</code></li>
<li><code>post[&quot;user_id&quot;] = @current_user[&quot;id&quot;]</code></li>
<li><code>Post.find_by({ &quot;id&quot; =&gt; params[&quot;id&quot;], &quot;user_id&quot; =&gt; @current_user[&quot;id&quot;] })</code></li>
</ul>
<p>With those tools, you have almost infinite control over the authorization logic in your applications. The hard part isn&apos;t the code. It&apos;s deciding who may do what, and writing it down - which, with an agent writing the code, is the product owner&apos;s job.</p>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[Cookies]]></title><description><![CDATA[<!--kg-card-begin: markdown--><p>If you haven&apos;t already, first read the <a href="https://entr451.com/encryption-and-data-security/">previous post</a> about Encryption &amp; Data Security.</p>
<p>You may already be aware of browser cookies - clearing a website&apos;s cookies is often a recommendation when a website isn&apos;t working properly. But what does that really mean?</p>
<p>Cookies</p>]]></description><link>https://entr451.com/cookies/</link><guid isPermaLink="false">6af5243fd9019e19441c3617</guid><dc:creator><![CDATA[Ben Block]]></dc:creator><pubDate>Thu, 12 Nov 2026 02:30:18 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><p>If you haven&apos;t already, first read the <a href="https://entr451.com/encryption-and-data-security/">previous post</a> about Encryption &amp; Data Security.</p>
<p>You may already be aware of browser cookies - clearing a website&apos;s cookies is often a recommendation when a website isn&apos;t working properly. But what does that really mean?</p>
<p>Cookies are made of key-value pairs, or, in Ruby parlance, a Hash. This hash of data is stored in the browser. Since it lives in the user&apos;s browser, it&apos;s specific to the user and to the website. And here&apos;s the important part: the browser sends a website&apos;s cookies back to it with <em>every</em> request. HTTP is stateless, but cookies let a website create a sense of state - a memory that lasts from one request to the next.</p>
<p>The flash message from the login is using this tool. When we assign a value to the <code>flash</code> hash, Rails stores it in a cookie for exactly one request: it&apos;s read on the very next request and then it&apos;s gone. That&apos;s how the message appears after the redirect and never again.</p>
<p>When it comes to users, we want to remember who the logged-in user is. We&apos;ll call this user the &quot;current user&quot;. Cookies are the perfect tool for this - when a user successfully authenticates, we store something that identifies them in a cookie, and then check for it on each new request. If it&apos;s there, we know who&apos;s logged in. If it isn&apos;t, nobody is.</p>
<h2 id="the-cookie-jar">The cookie jar</h2>
<p>Log in as a user you created. Then let&apos;s look at the cookies in the browser.</p>
<p>In Chrome, go to the <strong>View</strong> menu, then <strong>Developer</strong>, then <strong>Developer Tools</strong>. Chrome has many tools that are incredibly useful for web development. In the panel that opens, choose the <strong>Application</strong> tab. On the left, find <strong>Cookies</strong> and open it, then click the address that matches your address bar. Those are the cookies this browser holds for this website.</p>
<p>You&apos;ll see one called <code>_starter_app_session</code>, with a long, unreadable value. That&apos;s the session, and our user is in there. But you can&apos;t tell.</p>
<h2 id="why-not-just-a-cookie">Why not just a cookie?</h2>
<p>To see why it&apos;s unreadable, let&apos;s do it the obvious way first. In <code>sessions#create</code> (<code>app/controllers/sessions_controller.rb</code>), find the line that logs the user in:</p>
<pre><code class="language-ruby">session[&quot;user_id&quot;] = user[&quot;id&quot;]
</code></pre>
<p>and add a plain cookie right below it, for now:</p>
<pre><code class="language-ruby">cookies[&quot;user_id&quot;] = user[&quot;id&quot;]
</code></pre>
<p>Log out, log in again, and look at the cookies. There&apos;s a new <code>user_id</code> cookie, and its value is your id, in plain sight: <code>3</code>, or whatever your row&apos;s id is. On every request from now on, the browser tells our app who the current user is.</p>
<p>Before we go any further, there&apos;s a security vulnerability here. Double-click that value in the developer tools. You can change it to anything you want. If you were &quot;logged in&quot; as user 3, maybe you&apos;d like to be user 1. If our app trusted this cookie, by changing that number you&apos;d be logged in as someone else entirely. Imagine being logged in to Amazon and seeing your orders. Change one number, and now you&apos;re seeing someone else&apos;s orders. Not good.</p>
<p>Delete the <code>cookies[&quot;user_id&quot;]</code> line you added, then delete the <code>user_id</code> cookie in the developer tools (right click, <strong>Delete</strong>).</p>
<h2 id="the-session">The session</h2>
<p>That&apos;s why we use <code>session</code> instead. The session is a cookie too, but Rails encrypts and signs it with a secret only the server knows. You can&apos;t read what&apos;s in it, and if you change a single character, Rails throws the whole thing out and treats you as logged out. The browser carries it; only the server can open it.</p>
<p>So <code>session[&quot;user_id&quot;] = user[&quot;id&quot;]</code> is the line that logs a user in. It&apos;s in <code>sessions#create</code>, and it&apos;s also at the end of <code>users#create</code>, which is why signing up logs you in. Logging out, <code>sessions#destroy</code>, empties it:</p>
<pre><code class="language-ruby">def destroy
  # empty the session, then redirect
  session[&quot;user_id&quot;] = nil
  flash[&quot;notice&quot;] = &quot;Goodbye.&quot;
  redirect_to(&quot;/posts&quot;)
end
</code></pre>
<h2 id="current-user">Current user</h2>
<p>Putting the id in the session is half of it. On each request, we need to turn that id back into a user. Open the application controller (<code>app/controllers/application_controller.rb</code>):</p>
<pre><code class="language-ruby">class ApplicationController &lt; ActionController::Base
  # Runs before every action in every controller.
  before_action :find_current_user

  def find_current_user
    # the session is a small, signed box that travels with the browser as a cookie;
    # logging in puts the user&apos;s id in it, logging out empties it
    @current_user = User.find_by({ &quot;id&quot; =&gt; session[&quot;user_id&quot;] })
  end
end
</code></pre>
<p>Every controller in our app inherits from this one (look at the first line of <code>posts_controller.rb</code>: <code>class PostsController &lt; ApplicationController</code>). And <code>before_action</code> tells Rails to run <code>find_current_user</code> before every action, in every controller. So on every request, the app reads the user&apos;s id out of the session and looks up their row. If nobody is logged in, <code>session[&quot;user_id&quot;]</code> is <code>nil</code>, nothing is found, and <code>@current_user</code> is <code>nil</code>.</p>
<p>Because <code>@current_user</code> starts with <code>@</code>, every view can use it, including the layout. Look at the navbar in <code>app/views/layouts/application.html.erb</code>:</p>
<pre><code class="language-erb">&lt;nav&gt;
  &lt;a href=&quot;/&quot;&gt;Home&lt;/a&gt; |
  &lt;a href=&quot;/posts&quot;&gt;Tacostagram&lt;/a&gt; |
  &lt;% if @current_user %&gt;
    &lt;a href=&quot;/users/&lt;%= @current_user[&quot;id&quot;] %&gt;&quot;&gt;&lt;%= @current_user[&quot;first_name&quot;] %&gt;&lt;/a&gt; |
    &lt;a href=&quot;/logout&quot;&gt;Log out&lt;/a&gt;
  &lt;% else %&gt;
    &lt;a href=&quot;/users/new&quot;&gt;Sign up&lt;/a&gt; |
    &lt;a href=&quot;/login&quot;&gt;Log in&lt;/a&gt;
  &lt;% end %&gt;
&lt;/nav&gt;
</code></pre>
<p>Logged in, you see your name and <strong>Log out</strong>. Logged out, you see <strong>Sign up</strong> and <strong>Log in</strong>. Click around - the feed, a company, the data page - and your name stays in the navbar on every page, request after request. We can now, at long last, consider the current user as being logged in!</p>
<p>That <code>if @current_user</code> is also our first bit of authorization: the app shows a logged-in user something different than a visitor. Now that we know who the current user is, we can decide what they&apos;re allowed to do (<a href="https://entr451.com/authorization/">next post</a>).</p>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[Encryption & Data Security]]></title><description><![CDATA[<!--kg-card-begin: markdown--><p>If you haven&apos;t already, first read the <a href="https://entr451.com/authentication/">previous post</a> about Authentication.</p>
<p>At this point in our app, a user can sign up with their email and password and then log in with the same credentials. Their email may be widely known, but their password should (at least in</p>]]></description><link>https://entr451.com/encryption-and-data-security/</link><guid isPermaLink="false">6af52358d9019e19441c35f9</guid><dc:creator><![CDATA[Ben Block]]></dc:creator><pubDate>Thu, 12 Nov 2026 02:27:39 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><p>If you haven&apos;t already, first read the <a href="https://entr451.com/authentication/">previous post</a> about Authentication.</p>
<p>At this point in our app, a user can sign up with their email and password and then log in with the same credentials. Their email may be widely known, but their password should (at least in theory) be a secret. There are two points where that secret can be exposed:</p>
<ul>
<li>data in transit</li>
<li>data at rest</li>
</ul>
<p>Data &quot;in transit&quot; refers to when the data is moving through our app, which usually occurs when it&apos;s submitted via a form. Data &quot;at rest&quot; refers to how it&apos;s stored in the database. This doesn&apos;t just apply to passwords: when dealing with any sensitive data in your application, you want to think about its exposure both in transit and at rest.</p>
<h2 id="data-in-transit">Data in transit</h2>
<p>A password travels from the user&apos;s fingers to our database, and there are three places along the way where someone could see it.</p>
<p><strong>Over the user&apos;s shoulder.</strong> Look at the password input in the sign-up and login forms (<code>app/views/users/new.html.erb</code> and <code>app/views/sessions/new.html.erb</code>):</p>
<pre><code class="language-html">&lt;input type=&quot;password&quot; name=&quot;password&quot; id=&quot;password&quot;&gt;
</code></pre>
<p><code>type=&quot;password&quot;</code> is why you see dots instead of what you typed. It&apos;s perhaps more prone to typos now since the user can&apos;t see what they&apos;re typing, but it&apos;s also more secure. Tradeoffs... always tradeoffs. Notice that&apos;s <em>all</em> it does: it hides the password from the person behind you, and nothing else.</p>
<p><strong>On the wire.</strong> Between the browser and the server, the form&apos;s data crosses the internet. The lock icon in your browser&apos;s address bar means the connection is HTTPS: everything sent is encrypted on the way, and only the server can read it. Your Codespace already has it. When we deploy our own app, getting the certificate behind that lock is one of the steps.</p>
<p><strong>In the server log.</strong> Log in, then look at the bottom of the server log (the terminal running <code>bin/rails server</code>, or <code>log/development.log</code>). You&apos;ll see the form&apos;s data:</p>
<pre><code>Parameters: {&quot;email&quot; =&gt; &quot;ben@example.com&quot;, &quot;password&quot; =&gt; &quot;[FILTERED]&quot;}
</code></pre>
<p>The server log is just a text file of all the application&apos;s activity. It&apos;s easily read by anyone with access to the server, and often it&apos;s also sent to an outside service for debugging. So Rails hides anything that looks like a password before it writes the line. The list of what it hides is in <code>config/initializers/filter_parameter_logging.rb</code>:</p>
<pre><code class="language-ruby">Rails.application.config.filter_parameters += [
  :passw, :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn
]
</code></pre>
<p><code>:passw</code> matches any param whose name contains &quot;passw&quot;. If you ever have other data that you want to hide from the log (e.g. payment info), you add its name to this list and restart the server.</p>
<p>We&apos;re choosing not to hide the email address. Since it&apos;s personally identifiable information, it too may need to be protected. But when debugging, it can be helpful to search the logs for a user by their email. So this choice is a tradeoff, and you&apos;ll have to decide what&apos;s appropriate for your application.</p>
<p><strong>One more: guessing.</strong> The login form answers &quot;yes&quot; or &quot;no&quot; as fast as anyone asks it, and an attacker with a list of common passwords can ask a million times. Real apps slow down after a few wrong answers - a wait, a lockout, a code by email. How many tries and how long a wait is a product call, and it&apos;s the cheapest defense you&apos;ll ever buy.</p>
<h2 id="data-at-rest">Data at rest</h2>
<p>We&apos;ve hidden password data from view in the server log and the browser, but the passwords are still stored in our <code>users</code> table in plain text. Take a look:</p>
<pre><code>bin/rails runner scripts/show_data.rb User
</code></pre>
<p>And there, fully visible, are the users&apos; passwords.</p>
<p>It might go without saying, but to be explicitly clear... Storing passwords as plain text in the database is BAD. It&apos;s almost inevitable that, at some point, your application will go through a security breach of some kind. You might get hacked, or a backup ends up on a lost laptop, or you rely on another library of code that has a security vulnerability. Try as hard as you can to avoid it, but there&apos;s no such thing as 100% secure software. So how you prepare for and respond to a security breach is critically important.</p>
<p>In the case of a breach that exposes your database, you want to be able to state that &quot;yes, our database was compromised, but no sensitive information was accessible&quot;. The only way to be able to say so is to encrypt sensitive information (like passwords) so that even if it is accessed, it can never be read. In fact, the data should be encrypted in such a way that even you can&apos;t read it. Which also means: if a company can ever email you your password, they&apos;re storing it in a way they can read. Run.</p>
<h2 id="two-way-and-one-way">Two-way and one-way</h2>
<p>Data encryption comes in two common forms: two-way and one-way. In both, the process begins with an algorithm - a recipe - that turns the data into a new value. For example, a trivial encryption technique would be to replace every letter with the next letter in the alphabet. So &quot;apple&quot; becomes &quot;bqqmf&quot;. This would be a terrible encryption technique, but it gives you an idea of what encrypting data entails. In practice, we use well-known, industry-approved algorithms that are near impossible to crack.</p>
<p>With <strong>two-way</strong> encryption, the goal is to secure some data so that only an intended recipient can decrypt it and read the original. Any observers in between only see the encrypted data, which is indecipherable. That&apos;s the lock icon: HTTPS is two-way.</p>
<p>With <strong>one-way</strong> encryption (also called hashing), the goal is to secure some data so that it can never be decrypted and read. Although it can never be read, it can be checked - you can prove you know the original by encrypting it again and seeing that the results match.</p>
<p>Take a moment to think about these two approaches. Which makes the most sense for passwords in the database?</p>
<p>We never need to read a password back. We only need to check that the person logging in knows it. If the database is ever exposed, we want to be able to honestly say that no passwords were accessible. We want one-way.</p>
<p>To play with both, and to see how two-way encryption with a public and a private key keeps the lock icon safe, try <a href="https://benblock.me/encryption">this interactive explainer</a>.</p>
<h2 id="bcrypt">bcrypt</h2>
<p>In our application, we&apos;re going to use a one-way algorithm called <strong>bcrypt</strong>. It may seem counterintuitive to use a well-known, open-source tool for security, but that&apos;s exactly what we want. It&apos;s been tested and attacked by experts for decades, and the fact that it&apos;s public doesn&apos;t make it weaker. There are alternatives, but whichever you choose: unless you&apos;re a brilliant mathematician, never write your own.</p>
<p>Open the <code>Gemfile</code>. This is the list of the open-source libraries our application depends on, and they&apos;re installed when your Codespace is built (or when you run <code>bundle install</code>). <code>bcrypt</code> is already in it. If you&apos;re ever starting your own application from scratch, you&apos;ll add it here.</p>
<p>Now see it work. <code>scripts/encryption.rb</code> hashes the secret &quot;puppies&quot; twice, then checks some guesses against the hashes:</p>
<pre><code class="language-ruby">secret = &quot;puppies&quot;

first = BCrypt::Password.create(secret)
second = BCrypt::Password.create(secret)
puts first
puts second

puts BCrypt::Password.new(first) == &quot;puppies&quot;
puts BCrypt::Password.new(second) == &quot;puppies&quot;
puts BCrypt::Password.new(first) == &quot;kittens&quot;
</code></pre>
<p>Run it:</p>
<pre><code>bin/rails runner scripts/encryption.rb
</code></pre>
<p>You get two long, unintelligible strings, then <code>true</code>, <code>true</code>, <code>false</code>. Look closely at the two strings: they&apos;re different, even though both came from &quot;puppies&quot;. So how can &quot;puppies&quot; check out against both? Each hash has a random value mixed in, called a salt, and the salt is stored inside the hash itself. To check a guess, bcrypt reads the salt back out of the stored hash, hashes the guess with it, and compares. The hash is never decrypted, because it can&apos;t be.</p>
<p>The <code>==</code> there looks like a plain comparison, but it isn&apos;t: bcrypt is hashing the guess on the right before comparing. If this interests you, there are many more detailed explanations online. For our purposes, it&apos;s enough to know that the password can be safely stored, it can never be decrypted, and it can still be checked.</p>
<h2 id="hashing-our-passwords">Hashing our passwords</h2>
<p>Two edits: hash the password on its way in at sign-up, and check the hash at login.</p>
<p><strong>Sign-up.</strong> In <code>users#create</code> (<code>app/controllers/users_controller.rb</code>), the password is assigned straight into its column:</p>
<pre><code class="language-ruby">user[&quot;password&quot;] = params[&quot;password&quot;]
</code></pre>
<p>Instead, put it through bcrypt first, exactly the way <code>encryption.rb</code> did with &quot;puppies&quot;:</p>
<pre><code class="language-ruby">user[&quot;password&quot;] = BCrypt::Password.create(params[&quot;password&quot;])
</code></pre>
<p>Sign up as a new user, then look at the data again. Your new row&apos;s <code>password</code> is a long bcrypt string, starting with <code>$2a$</code>, and not what you typed. The password is no longer exposed, and since we used one-way encryption it can never be decrypted - even developers with access have no way of reading user passwords.</p>
<p>And this is the moment to notice: for one line, at the top of <code>create</code>, we <em>do</em> have the password as typed. We could email it to the user right there. We never do: after this line, it should never exist anywhere as typed again.</p>
<p><strong>Login.</strong> Log out, then try logging in as the user you just made. It fails: <code>sessions#create</code> is still comparing what you typed with the <code>password</code> column, which is now a hash. Your password isn&apos;t &quot;hockey&quot; anymore as far as the database knows. It&apos;s <code>$2a$12$...</code>. Change the check to the same comparison <code>encryption.rb</code> made:</p>
<pre><code class="language-ruby"># 2. check the password
if user &amp;&amp; BCrypt::Password.new(user[&quot;password&quot;]) == params[&quot;password&quot;]
</code></pre>
<p><code>BCrypt::Password.new</code> reads the stored hash (salt included), and <code>==</code> hashes what was typed with that salt and compares. Log in as your new user again: it works. And a wrong password still fails.</p>
<p>If you fell behind, <code>code-along-complete/2-at-rest/</code> has these two files finished.</p>
<h2 id="the-shortcut-hassecurepassword">The shortcut: has_secure_password</h2>
<p>Every app with passwords does what we just did, so Rails has it built in. It lives in the model, because the model is where the rules for a user belong. Open <code>app/models/user.rb</code> and add one line:</p>
<pre><code class="language-ruby">class User &lt; ApplicationRecord
  # Gives the model a password= method that hashes what it is given into the
  # password_digest column, and an authenticate(guess) method that checks a guess
  # against that hash. The plain password column is no longer written.
  has_secure_password
end
</code></pre>
<p>It&apos;s bcrypt underneath, the same two steps, with two differences:</p>
<ul>
<li>
<p>The hash goes in a column called <code>password_digest</code> (&quot;digest&quot; is another word for the hashed value), not <code>password</code>. That&apos;s why our <code>users</code> table has that column: <code>has_secure_password</code> expects a column with exactly that name.</p>
</li>
<li>
<p>You don&apos;t call <code>BCrypt::Password</code> yourself. Sign-up becomes:</p>
<pre><code class="language-ruby"># the password goes through the model, which hashes it (see app/models/user.rb)
user.password = params[&quot;password&quot;]
</code></pre>
<p>This is the one place we don&apos;t use the <code>user[&quot;column&quot;]</code> style, because <code>password=</code> isn&apos;t a column: it&apos;s the method <code>has_secure_password</code> gave the model. And login becomes:</p>
<pre><code class="language-ruby"># 2. check the password
if user &amp;&amp; user.authenticate(params[&quot;password&quot;])
</code></pre>
</li>
</ul>
<p>This version, all three files, is in <code>code-along-complete/2-at-rest-has-secure-password/</code>. It&apos;s what most Rails apps use. Use one or the other, not both. Either way works; what matters is that you know what the shortcut does for you.</p>
<h2 id="the-users-who-were-already-here">The users who were already here</h2>
<p>Now log in as Ben (<code>ben@example.com</code>, <code>tacos123</code>).</p>
<p>If you did it the first way, with <code>BCrypt::Password</code> yourself, the page breaks:</p>
<pre><code>BCrypt::Errors::InvalidHash
invalid hash
</code></pre>
<p>Ben&apos;s row was saved before any of this existed. His <code>password</code> is still &quot;tacos123&quot; as typed, and <code>BCrypt::Password.new</code> expects a bcrypt hash, so it gives up. (With <code>has_secure_password</code>, it fails more quietly: Ben&apos;s <code>password_digest</code> is empty, so there&apos;s nothing to check, and he just gets &quot;Nope.&quot;) Brian is locked out too.</p>
<p>We made a good change to how we store data, and the rows that were already there didn&apos;t come with us. In this app, it&apos;s two users. In a real company, it&apos;s thousands, and they&apos;re your customers. That&apos;s known as <a href="https://entr451.com/technical-debt/">technical debt</a>.</p>
<h2 id="a-note-on-the-future">A note on the future</h2>
<p>HTTPS, the two-way encryption behind the lock icon, rests on math that a large enough quantum computer could undo. Traffic recorded today could be read years from now, which is why new standards are already rolling out. Password hashes hold up much better. And AI finds software bugs faster; it doesn&apos;t break the math. For you, as a manager, this is a patching and vendor question, not a rewrite.</p>
<h2 id="logged-in">Logged in?</h2>
<p>Our basic authentication scheme is now complete - a user can sign up, we hash their secret password, and we check their credentials on login and respond appropriately. But how does the user <em>stay</em> logged in? Remember that HTTP is stateless - each request has no knowledge of the requests before it. So even when the credentials are right, the server would forget all about it as soon as the next request begins.</p>
<p>How does the app know, request after request, that a user is logged in? To answer that, we need to revisit browser state and learn about another browser tool: cookies (<a href="https://entr451.com/cookies/">next post</a>).</p>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[Authentication]]></title><description><![CDATA[<!--kg-card-begin: markdown--><p>If you haven&apos;t already, first read the <a href="https://entr451.com/users/">previous post</a> about Users.</p>
<p>The basic authentication scheme that we&apos;ll use depends on a unique identifier (an email) and a secret value (a password). We will check that a user knows the exact combination of these two credentials, thus</p>]]></description><link>https://entr451.com/authentication/</link><guid isPermaLink="false">6af52253d9019e19441c35d5</guid><dc:creator><![CDATA[Ben Block]]></dc:creator><pubDate>Thu, 12 Nov 2026 02:22:49 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><p>If you haven&apos;t already, first read the <a href="https://entr451.com/users/">previous post</a> about Users.</p>
<p>The basic authentication scheme that we&apos;ll use depends on a unique identifier (an email) and a secret value (a password). We will check that a user knows the exact combination of these two credentials, thus &quot;authenticating&quot; that user into the app.</p>
<p>It&apos;s worth noting that there are many other authentication schemes:</p>
<ul>
<li><strong>Log in with Google</strong> (or GitHub, or LinkedIn): the app sends you to another service, lets you log in there, and trusts its answer. This is OAuth.</li>
<li><strong>A code by text, email or an authenticator app</strong>, on top of the password: multi-factor authentication (MFA, or 2FA).</li>
<li><strong>A magic link</strong>: no password at all, just a link emailed to you that logs you in once.</li>
<li><strong>Single sign-on</strong>: your company&apos;s login, used for every tool at work.</li>
<li><strong>Passkeys</strong>: your phone or laptop proves it&apos;s you, with your face or fingerprint, and there&apos;s no password to steal.</li>
</ul>
<p>There are tradeoffs to using any scheme. For example, more security often means more friction for the user, and logging in with someone else&apos;s service means sharing data with them. So you need to find an appropriate balance in your application.</p>
<h2 id="the-routes">The routes</h2>
<p>The period of time when a user is logged in is commonly referred to as a &quot;session&quot;. A session isn&apos;t database-backed - there&apos;s no <code>sessions</code> table - so this is one of the rare places where we break the pattern on purpose. Open <code>config/routes.rb</code> and look at the bottom:</p>
<pre><code class="language-ruby">get(&quot;/login&quot;, { :controller =&gt; &quot;sessions&quot;, :action =&gt; &quot;new&quot; })
post(&quot;/login&quot;, { :controller =&gt; &quot;sessions&quot;, :action =&gt; &quot;create&quot; })
get(&quot;/logout&quot;, { :controller =&gt; &quot;sessions&quot;, :action =&gt; &quot;destroy&quot; })
</code></pre>
<p>Three routes, written out by hand instead of a <code>resources</code> line, because there&apos;s nothing to list, show or edit, and <code>/login</code> reads better than <code>/sessions/new</code>. Each one still goes to a controller and an action, just like the seven routes a <code>resources</code> line makes.</p>
<h2 id="the-login-form">The login form</h2>
<p>Click <strong>Log in</strong> in the navbar. The <code>new</code> action in <code>app/controllers/sessions_controller.rb</code> renders <code>app/views/sessions/new.html.erb</code>:</p>
<pre><code class="language-erb">&lt;h1&gt;Log in&lt;/h1&gt;

&lt;form action=&quot;/login&quot; method=&quot;post&quot;&gt;
  &lt;p&gt;
    &lt;label for=&quot;email&quot;&gt;Email&lt;/label&gt;
    &lt;input type=&quot;text&quot; name=&quot;email&quot; id=&quot;email&quot;&gt;
  &lt;/p&gt;
  &lt;p&gt;
    &lt;label for=&quot;password&quot;&gt;Password&lt;/label&gt;
    &lt;input type=&quot;password&quot; name=&quot;password&quot; id=&quot;password&quot;&gt;
  &lt;/p&gt;
  &lt;button&gt;Log in&lt;/button&gt;
&lt;/form&gt;
</code></pre>
<p>The form makes a POST request to <code>/login</code>, and the second route above sends that to the <code>create</code> action. Try logging in as <code>ben@example.com</code> with the password <code>tacos123</code>. You get &quot;Nope.&quot; no matter what you type, because <code>create</code> doesn&apos;t do anything yet:</p>
<pre><code class="language-ruby">def create
  # 1. find the User whose email was typed in the form
  # 2. check the password
  # 3. if both hold, put the user&apos;s id in the session, then redirect to the posts
  # 4. otherwise redirect back to the login form
  flash[&quot;notice&quot;] = &quot;Nope.&quot;
  redirect_to(&quot;/login&quot;)
end
</code></pre>
<p>In other resources, <code>create</code> is where we would use the model to insert a row into the table. That doesn&apos;t make sense for a session. Instead, we check the form data against the data in the <code>users</code> table, and if it matches, log the user in. Let&apos;s fill in the comments.</p>
<h2 id="step-1-find-the-user">Step 1: find the user</h2>
<p>Since email is a unique identifier, we can use it to find a row in the <code>users</code> table. Look at the server log for the request you just made and you&apos;ll see the form&apos;s data in the params: <code>&quot;email&quot; =&gt; &quot;ben@example.com&quot;</code>. It&apos;s at <code>params[&quot;email&quot;]</code>.</p>
<pre><code class="language-ruby"># 1. find the User whose email was typed in the form
user = User.find_by({ &quot;email&quot; =&gt; params[&quot;email&quot;] })
</code></pre>
<p>Submit the form again and watch the log. You&apos;ll see the request, and then the SQL query for a user with that email:</p>
<pre><code>SELECT &quot;users&quot;.* FROM &quot;users&quot; WHERE &quot;users&quot;.&quot;email&quot; = ? LIMIT ?
</code></pre>
<p>Notice the <code>?</code>. Keep it in mind; we&apos;ll come back to why it&apos;s there when we talk about injection.</p>
<h2 id="step-2-check-the-password">Step 2: check the password</h2>
<p>If there is a user with that email, we compare the password they typed with the password in the row. <code>find_by</code> returns <code>nil</code> when there&apos;s no match, so we check for a user first:</p>
<pre><code class="language-ruby"># 2. check the password
if user &amp;&amp; user[&quot;password&quot;] == params[&quot;password&quot;]
</code></pre>
<h2 id="steps-3-and-4-let-them-in-or-dont">Steps 3 and 4: let them in, or don&apos;t</h2>
<p>If both hold, the user is who they say they are. We remember that, then send them to the posts. Otherwise, back to the form:</p>
<pre><code class="language-ruby">def create
  # 1. find the User whose email was typed in the form
  user = User.find_by({ &quot;email&quot; =&gt; params[&quot;email&quot;] })
  # 2. check the password
  if user &amp;&amp; user[&quot;password&quot;] == params[&quot;password&quot;]
    # 3. if both hold, put the user&apos;s id in the session, then redirect to the posts
    session[&quot;user_id&quot;] = user[&quot;id&quot;]
    flash[&quot;notice&quot;] = &quot;Welcome back, #{user[&quot;first_name&quot;]}.&quot;
    redirect_to(&quot;/posts&quot;)
  else
    # 4. otherwise redirect back to the login form
    flash[&quot;notice&quot;] = &quot;Nope.&quot;
    redirect_to(&quot;/login&quot;)
  end
end
</code></pre>
<p>Test it out with correct and incorrect data. Log in as Ben and you land on the posts with &quot;Welcome back, Ben.&quot; and his name in the navbar. Get the password wrong and you&apos;re back at the form.</p>
<p><code>session[&quot;user_id&quot;] = user[&quot;id&quot;]</code> is the line that actually logs the user in. It&apos;s the same line the sign-up action ends with, which is how signing up logs you in too. How it works is a post of its own (<a href="https://entr451.com/cookies/">Cookies</a>). <strong>Log out</strong> in the navbar runs<br>
<code>sessions#destroy</code>, which sets it back to <code>nil</code>.</p>
<p>If you fell behind, <code>code-along-complete/1-login/</code> in your copy has this file finished.</p>
<h2 id="one-message-on-purpose">One message, on purpose</h2>
<p>Notice that a wrong email and a wrong password get the same message: &quot;Nope.&quot; It would be friendlier to say &quot;We don&apos;t have an account with that email&quot; - and it would also tell anyone who types an email into your login form whether that person has an account. Sometimes that&apos;s a secret worth keeping (a dating app, a health app), sometimes it isn&apos;t. It&apos;s a product decision, and like most security decisions it&apos;s a tradeoff between convenience and safety.</p>
<h2 id="flash">Flash</h2>
<p>How did that message display on the next page? Look at the layout that wraps every view, <code>app/views/layouts/application.html.erb</code>:</p>
<pre><code class="language-erb">&lt;% if flash[&quot;notice&quot;] %&gt;
  &lt;p&gt;&lt;em&gt;&lt;%= flash[&quot;notice&quot;] %&gt;&lt;/em&gt;&lt;/p&gt;
&lt;% end %&gt;
</code></pre>
<p>That&apos;s the <code>notice</code> key in the <code>flash</code> hash. When it has a value (because it was assigned in <code>create</code> before redirecting), the value is displayed. The message only appears on the very next request - refresh, and it&apos;s gone. Any HTML that we want displayed on every page belongs in this layout file, like the navbar.</p>
<p>It&apos;s understandable if you&apos;re confused about how this works - it appears to break the &quot;HTTP is stateless&quot; rule, since the message shows up on the request <em>after</em> the form submission. Spoiler alert: the same tool that carries this message is what remembers that a user is logged in.</p>
<p>But before we get there, there&apos;s a problem. Our login checks the password by comparing it with the password in the table, which means the password is sitting in the table, in plain text. Let&apos;s shore up some security vulnerabilities in our code first (<a href="https://entr451.com/encryption-and-data-security/">next post</a>).</p>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[Users]]></title><description><![CDATA[<!--kg-card-begin: markdown--><p>Unless you&apos;re building an application entirely for your own use - or an application where all data is created anonymously and globally visible - at some point, you will need to think about users. In fact, when designing an application, we spend time upfront thinking about the user&</p>]]></description><link>https://entr451.com/users/</link><guid isPermaLink="false">6af4f952d9019e19441c35b8</guid><dc:creator><![CDATA[Ben Block]]></dc:creator><pubDate>Wed, 11 Nov 2026 23:28:03 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><p>Unless you&apos;re building an application entirely for your own use - or an application where all data is created anonymously and globally visible - at some point, you will need to think about users. In fact, when designing an application, we spend time upfront thinking about the user&apos;s perspective when we write user stories - i.e. how will a user experience and navigate the application.</p>
<p>Users are, in some ways, just like any other resource in an application, with the same seven actions as posts or companies. So let&apos;s start there.</p>
<h2 id="getting-set-up">Getting set up</h2>
<p>Head over to <a href="https://github.com/dsgn425-fall2026/auth">https://github.com/dsgn425-fall2026/auth</a> and choose <strong>Use this template</strong>, then <strong>Create a new repository</strong>, in your own account, and choose <strong>Private</strong>. On your copy, choose <strong>Code</strong>, then <strong>Codespaces</strong>, then <strong>Create codespace on main</strong>. When it&apos;s up, start the server in the terminal:</p>
<pre><code>bin/rails server
</code></pre>
<p>Open port 3000 in the browser. The home page lists every page in the app; click <strong>Tacostagram</strong>.</p>
<h2 id="whats-new">What&apos;s new</h2>
<p>This application picks up where web-apps-2 left off: Tacostagram, where anyone can post a taco and anyone can read the feed. Companies and contacts are still here too. What&apos;s new is users.</p>
<p>Look at the navbar at the top of every page: <strong>Sign up</strong> and <strong>Log in</strong>. Open the data page (<code>/data</code>) and you&apos;ll see a <code>users</code> table with two rows already in it. Its columns are <code>first_name</code>, <code>last_name</code>, <code>email</code> and <code>password</code> (and <code>password_digest</code>, which we&apos;ll get to in a later post). Nothing new here that we haven&apos;t already seen.</p>
<p>Next, take a look at the users controller (<code>app/controllers/users_controller.rb</code>). This code should also be familiar. There&apos;s a <code>show</code> action that displays one user. And there are <code>new</code> and <code>create</code> actions to display a sign-up form, then receive the data from that form and insert it into the database:</p>
<pre><code class="language-ruby">def create
  # start with a new User
  user = User.new
  # assign the form&apos;s values to the User&apos;s columns
  user[&quot;first_name&quot;] = params[&quot;first_name&quot;]
  user[&quot;last_name&quot;] = params[&quot;last_name&quot;]
  user[&quot;email&quot;] = params[&quot;email&quot;]
  user[&quot;password&quot;] = params[&quot;password&quot;]
  # save the User row
  user.save
  # log the new user in, then redirect
  session[&quot;user_id&quot;] = user[&quot;id&quot;]
  flash[&quot;notice&quot;] = &quot;Welcome, #{user[&quot;first_name&quot;]}.&quot;
  redirect_to(&quot;/posts&quot;)
end
</code></pre>
<p>This isn&apos;t any different than creating a company or a post. The last three lines are new, and we&apos;ll come back to them. However, conceptually this is more than merely the C in CRUD for a user - it represents an almost ubiquitous user story in every application:</p>
<ul>
<li>As a visitor, I want to create my own account with an email and password.</li>
</ul>
<p>This is a very basic version of the user story, but you&apos;ll see some variant of it in almost every application. Sometimes account creation is referred to as registration or sign-up. Sometimes a username is required instead of, or in addition to, an email. Regardless, the expected behavior is the same - a non-user becomes a user by submitting some credentials that can later be used for identification.</p>
<h2 id="try-it">Try it</h2>
<p>Click <strong>Sign up</strong>, fill out the form and submit. You land on the posts with a welcome message, and your first name is in the navbar. Click your name: that&apos;s your account page, <code>/users/3</code> or whatever your id is. It shows your name and email.</p>
<p>What isn&apos;t displayed?</p>
<h2 id="passwords">Passwords</h2>
<p>The password is missing. That&apos;s very intentional - when deciding what information to display on a resource&apos;s show page, you don&apos;t have to (and rarely) show everything. You&apos;ll need to decide how to handle sensitive information in your applications, but as we&apos;ll soon see, passwords should never be exposed.</p>
<p>Not showing it isn&apos;t the same as protecting it, though. Open <code>/data</code> again, or in a second terminal run:</p>
<pre><code>bin/rails runner scripts/show_data.rb User
</code></pre>
<p>And there, in plain text, you&apos;ll see the password you just signed up with, next to the two users who were there before you (<code>ben@example.com</code> with <code>tacos123</code>, and <code>brian@example.com</code> with <code>password</code>). To reiterate, anyone with access to the database - an employee, a backup file, a hacker - has access to all the user passwords! &#x1F631;</p>
<p>We&apos;ll come back to this in a bit when we start fixing the security vulnerabilities, but for now we&apos;ll just put a pin in it. A user can sign up with their email and password. These are our app&apos;s authentication credentials - meaning, the data a user will use to identify themselves when<br>
they log in.</p>
<h2 id="authentication-and-authorization">Authentication and authorization</h2>
<p>When dealing with users in a software application, there are generally two steps to consider: authentication and authorization.</p>
<p>Think of an airport. At security, they check your ID against your face: are you who you say you are? That&apos;s <strong>authentication</strong>. Once you&apos;re through, you can walk around the terminal, but your boarding pass only gets you onto one plane, and only some passengers get into the lounge. What you&apos;re allowed to do once you&apos;re in is <strong>authorization</strong>.</p>
<p>Or more simply, authentication asks &quot;who is using the application?&quot; and authorization asks &quot;what can this user do within the application?&quot; We&apos;ll start with authentication (<a href="https://entr451.com/authentication/">next post</a>).</p>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[Opening and reviewing a pull request]]></title><description><![CDATA[<!--kg-card-begin: markdown--><p>In week one you added one file to the class roster and it went in through a pull request. That was the smallest possible change, in a shared repository, with the computer refusing to let anyone skip the review. From here on the change is a feature, the repository is</p>]]></description><link>https://entr451.com/opening-and-reviewing-a-pull-request/</link><guid isPermaLink="false">6aead120d9019e19441c3586</guid><dc:creator><![CDATA[Ben Block]]></dc:creator><pubDate>Wed, 04 Nov 2026 06:33:04 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><p>In week one you added one file to the class roster and it went in through a pull request. That was the smallest possible change, in a shared repository, with the computer refusing to let anyone skip the review. From here on the change is a feature, the repository is yours, and nobody enforces the rule but you. The steps are the same.</p>
<h2 id="1-start-from-a-ticket">1. Start from a ticket</h2>
<p>A ticket is a GitHub issue: a short story about who wants what and why, and under it a definition of done, a few sentences that say when the feature is finished. In the class repo the tickets are already in the <strong>Issues</strong> tab. For your own project you write them: one issue per feature, three to five checkable sentences each.</p>
<p>Read the ticket before you touch anything. It is what your reviewer will hold the change against.</p>
<h2 id="2-branch-first">2. Branch first</h2>
<p>Never build on <code>main</code>. In the bottom-left corner of the Codespace, click <code>main</code>, choose <strong>Create new branch</strong>, type <code>posts-form</code>, and press Enter. The corner now reads <code>posts-form</code>, and everything you commit from here goes on that branch. (Source Control&apos;s <strong>...</strong> menu has the same thing under <strong>Branch</strong>, <strong>Create Branch</strong>.)</p>
<p>The name describes the change. If you forget and commit on <code>main</code>, nothing is lost: make the branch now the same way, and your commit comes with it.</p>
<h2 id="3-build-it-with-tests">3. Build it, with tests</h2>
<p>Before the prompt, one line in <code>AGENTS.md</code>, the file your agent reads before it does anything. It already says that every sentence of a definition of done gets a test, and it points at the code rules; the agent already knows how to read a GitHub issue. Add the one thing nobody said, at the end of the Rules list:</p>
<pre><code>- After changing any code, run the tests and report the result before saying you are done.
</code></pre>
<p>Then the prompt can be short, because the file carries the rest:</p>
<pre><code>Read issue #1 and build it.
</code></pre>
<p>Watch what happens that you did not ask for: the agent reads the ticket, writes one named test per sentence, runs them and reports. That is what the file is for. Anything you would otherwise type into every prompt belongs there instead.</p>
<p>Then check it yourself, twice: use the page in the browser, and run the tests:</p>
<pre><code>bin/rails test
</code></pre>
<p>Open the test file and read the test names against the ticket. One per sentence? A sentence with no test is a sentence nobody checked.</p>
<h2 id="4-commit-and-publish">4. Commit and publish</h2>
<p>In Source Control, stage the changes and commit with a message that says what changed, so that someone reading the history later knows without opening the diff. &quot;Add the new post form and create action, with tests&quot; is a message. &quot;done&quot; and &quot;tried again&quot; are not.</p>
<p>Then <strong>Publish Branch</strong>. Your branch is now on GitHub; <code>main</code> is untouched.</p>
<h2 id="5-open-the-pull-request">5. Open the pull request</h2>
<p>On your repo&apos;s GitHub page a banner offers <strong>Compare &amp; pull request</strong>. Click it. The title is the ticket&apos;s title. In the body, write <code>closes #1</code> (the issue number), so merging the PR closes the ticket, and a sentence on what you did. <strong>Create pull request</strong>.</p>
<p>A pull request is a request to change <code>main</code>. Nothing has changed yet.</p>
<h2 id="6-invite-your-reviewer">6. Invite your reviewer</h2>
<p>Your copy of the repo is private, so only a collaborator can review it. On the repo page: <strong>Settings</strong>, <strong>Collaborators</strong>, <strong>Add people</strong>, your partner&apos;s GitHub username, <strong>Add</strong>. They accept from the email GitHub sends, or at github.com/notifications.</p>
<h2 id="7-review-someone-elses">7. Review someone else&apos;s</h2>
<p>Open their pull request. Read the issue it closes, then click <strong>Files changed</strong> and read the diff. If you are sitting next to them, run the tests on their machine:</p>
<pre><code>bin/rails test
</code></pre>
<p>Then answer five questions:</p>
<ol>
<li>Does the change do what the issue says? Read the issue, then the diff.</li>
<li>Does the commit message tell you what changed without opening the diff?</li>
<li>Does it follow the pattern? The right actions in the right controller, the form posts to the resource&apos;s path, <code>create</code> redirects.</li>
<li>Is there a test for every sentence in the issue, and do they pass when you run them?</li>
<li>Would you merge it? If not, what would you ask for?</li>
</ol>
<p>The first, second and fifth are week one&apos;s three questions. They never change. The third and fourth are new because the change is code now, built against a pattern and a ticket.</p>
<p>Leave at least one comment on a line: hover over a line in the diff, click the blue <strong>+</strong>, write it, <strong>Add single comment</strong>. Then <strong>Review changes</strong>. If all five answers are yes, <strong>Approve</strong>. If not, <strong>Request changes</strong> and say which question failed. <strong>Submit review</strong>.</p>
<h2 id="8-merge">8. Merge</h2>
<p>When your reviewer approves, <strong>Merge pull request</strong>, <strong>Confirm merge</strong>, <strong>Delete branch</strong>. The feature is on <code>main</code>, and the issue it closed is closed.</p>
<p>The merge button worked before anyone reviewed. On a personal repo, GitHub does not stop you. The rule held because you held it. On a team, the repository is set up so the computer holds it too, which is what the roster did in week one.</p>
<h2 id="if-you-requested-changes">If you requested changes</h2>
<p>The author fixes it on the same branch, commits, and syncs. The pull request updates on its own. Review again. That loop, change, review, change, merge, is the part of this course that carries the furthest, and the one an agent cannot do for you: someone who did not write the code has to read it.</p>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[Web Apps 2 - Issue 1 - Solution]]></title><description><![CDATA[<!--kg-card-begin: markdown--><h2 id="the-ticket">The ticket</h2>
<p>Issue #1 in your copy of <code>web-apps-2</code>:</p>
<blockquote>
<p><strong>Post a taco</strong><br>
As a Tacostagram user, I want to post a photo of my tacos with a caption, so everyone can see what I&apos;m eating.</p>
</blockquote>
<blockquote>
<p><strong>Definition of done</strong></p>
<ul>
<li>The new post page shows a form with fields for</li></ul></blockquote>]]></description><link>https://entr451.com/web-apps-2-posts-solution/</link><guid isPermaLink="false">6aeaca56d9019e19441c3561</guid><dc:creator><![CDATA[Ben Block]]></dc:creator><pubDate>Wed, 04 Nov 2026 06:05:15 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><h2 id="the-ticket">The ticket</h2>
<p>Issue #1 in your copy of <code>web-apps-2</code>:</p>
<blockquote>
<p><strong>Post a taco</strong><br>
As a Tacostagram user, I want to post a photo of my tacos with a caption, so everyone can see what I&apos;m eating.</p>
</blockquote>
<blockquote>
<p><strong>Definition of done</strong></p>
<ul>
<li>The new post page shows a form with fields for the author, the body and the image.</li>
<li>Submitting the form creates a post with the author, body and image that were entered.</li>
<li>After submitting, the user lands on the posts page and sees the new post there.</li>
</ul>
</blockquote>
<p>The ticket is the whole specification. Your agent read it, built it, and wrote one test per sentence. Yours will not match this line for line, and should not. What should match is the shape: two actions, one form, three tests, and a pull request that a classmate reviewed against the ticket.</p>
<h2 id="what-the-agent-built">What the agent built</h2>
<h3 id="the-controller">The controller</h3>
<p><code>new</code> and <code>create</code> were already there as comments. The agent filled them in, keeping each step comment:</p>
<pre><code class="language-ruby">def new
  # render posts/new view, the form for a new Post
  render({ :template =&gt; &quot;posts/new&quot; })
end

def create
  # start with a new Post
  post = Post.new
  # assign the form&apos;s values to the Post&apos;s columns
  post[&quot;author&quot;] = params[&quot;author&quot;]
  post[&quot;body&quot;] = params[&quot;body&quot;]
  post[&quot;image&quot;] = params[&quot;image&quot;]
  # save the Post row
  post.save
  # redirect the user
  redirect_to(&quot;/posts&quot;)
end
</code></pre>
<p><code>new</code> only renders the form. <code>create</code> has no view: it saves and sends the user to a page that already exists. The three keys in <code>params</code> are the three <code>name</code> attributes in the form below.</p>
<h3 id="the-form">The form</h3>
<p><code>app/views/posts/new.html.erb</code> had a skeleton with a comment where the fields go:</p>
<pre><code class="language-erb">&lt;h1&gt;New post&lt;/h1&gt;

&lt;form action=&quot;/posts&quot; method=&quot;post&quot;&gt;
  &lt;p&gt;
    &lt;label for=&quot;author&quot;&gt;Author&lt;/label&gt;
    &lt;input type=&quot;text&quot; name=&quot;author&quot; id=&quot;author&quot;&gt;
  &lt;/p&gt;
  &lt;p&gt;
    &lt;label for=&quot;body&quot;&gt;Body&lt;/label&gt;
    &lt;input type=&quot;text&quot; name=&quot;body&quot; id=&quot;body&quot;&gt;
  &lt;/p&gt;
  &lt;p&gt;
    &lt;label for=&quot;image&quot;&gt;Image&lt;/label&gt;
    &lt;input type=&quot;text&quot; name=&quot;image&quot; id=&quot;image&quot;&gt;
  &lt;/p&gt;
  &lt;button&gt;Post it&lt;/button&gt;
&lt;/form&gt;

&lt;p&gt;&lt;a href=&quot;/posts&quot;&gt;Back to all posts&lt;/a&gt;&lt;/p&gt;
</code></pre>
<p><code>action=&quot;/posts&quot;</code> and <code>method=&quot;post&quot;</code> are the <code>create</code> row of the pattern. No routes were added: <code>resources(&quot;posts&quot;)</code> in <code>config/routes.rb</code> already made all seven.</p>
<h3 id="the-tests">The tests</h3>
<p>One per sentence, named with the sentence, in <code>test/integration/posts_page_test.rb</code>:</p>
<pre><code class="language-ruby">require &quot;test_helper&quot;

class PostsPageTest &lt; ActionDispatch::IntegrationTest
  test &quot;The new post page shows a form with fields for the author, the body and the image.&quot; do
    get(&quot;/posts/new&quot;)

    assert_response(:success)
    assert_select(&quot;form[action=&apos;/posts&apos;][method=&apos;post&apos;]&quot;) do
      assert_select(&quot;input[name=&apos;author&apos;]&quot;)
      assert_select(&quot;input[name=&apos;body&apos;]&quot;)
      assert_select(&quot;input[name=&apos;image&apos;]&quot;)
    end
  end

  test &quot;Submitting the form creates a post with the author, body and image that were entered.&quot; do
    post(&quot;/posts&quot;, :params =&gt; {
      &quot;author&quot; =&gt; &quot;Ben&quot;,
      &quot;body&quot; =&gt; &quot;Al pastor on a Tuesday&quot;,
      &quot;image&quot; =&gt; &quot;https://images.unsplash.com/photo-tacos&quot;
    })

    created = Post.find_by({ &quot;author&quot; =&gt; &quot;Ben&quot; })
    assert_not_nil(created)
    assert_equal(&quot;Al pastor on a Tuesday&quot;, created[&quot;body&quot;])
    assert_equal(&quot;https://images.unsplash.com/photo-tacos&quot;, created[&quot;image&quot;])
  end

  test &quot;After submitting, the user lands on the posts page and sees the new post there.&quot; do
    post(&quot;/posts&quot;, :params =&gt; {
      &quot;author&quot; =&gt; &quot;Ben&quot;,
      &quot;body&quot; =&gt; &quot;Al pastor on a Tuesday&quot;,
      &quot;image&quot; =&gt; &quot;https://images.unsplash.com/photo-tacos&quot;
    })

    assert_redirected_to(&quot;/posts&quot;)
    follow_redirect!
    assert_select(&quot;h3&quot;, &quot;Ben&quot;)
    assert_select(&quot;p&quot;, &quot;Al pastor on a Tuesday&quot;)
    assert_select(&quot;img[src=&apos;https://images.unsplash.com/photo-tacos&apos;]&quot;)
  end
end
</code></pre>
<pre><code>bin/rails test
3 runs, 21 assertions, 0 failures, 0 errors, 0 skips
</code></pre>
<p>Read each test against its sentence. The first fetches the form page and looks for the three inputs. The second submits the form and looks the row up in the table. The third submits, follows the redirect, and looks for the post on the page. Each one checks exactly what its sentence says, and nothing it doesn&apos;t.</p>
<h2 id="reviewing-it">Reviewing it</h2>
<p>The five questions from class, answered for this change:</p>
<ol>
<li><strong>Does it do what the issue says?</strong> The three sentences are the three tests, and they pass.</li>
<li><strong>Does the commit message tell you what changed?</strong> Yours to judge. &quot;Add the new post form and create action, with tests&quot; would.</li>
<li><strong>Does it follow the pattern?</strong> <code>new</code> and <code>create</code> in <code>PostsController</code>, the form posts to <code>/posts</code>, <code>create</code> redirects. Yes.</li>
<li><strong>A test per sentence, passing when you run them?</strong> Three sentences, three tests, green.</li>
<li><strong>Would you merge it?</strong> Yes. One thing you might ask for: the image is a text box the user has to paste an address into. That is what the ticket asked for; a file upload is week ten.</li>
</ol>
<h2 id="issue-2-if-you-got-to-it">Issue #2, if you got to it</h2>
<p>&quot;Add a contact to a company&quot; is the same loop with one new piece: the company&apos;s id has to travel from the company&apos;s page, through the form, to <code>create</code>, without the user typing it. That is a hidden field, and the &quot;Handling relationships&quot; section of <a href="https://entr451.com/creating-new-records/">Creating new records</a> walks through it.</p>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[From an address to a page: routes, controllers and views]]></title><description><![CDATA[<!--kg-card-begin: markdown--><p>A browser asks for an address, and a server sends back HTML. That&apos;s the whole of the web, and it hasn&apos;t changed since 1992. This post follows one request through a Rails app: the address you type, the three files Rails reads to answer it, and</p>]]></description><link>https://entr451.com/from-address-to-page/</link><guid isPermaLink="false">6ae118f3d9019e19441c3545</guid><dc:creator><![CDATA[Ben Block]]></dc:creator><pubDate>Tue, 27 Oct 2026 21:36:53 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><p>A browser asks for an address, and a server sends back HTML. That&apos;s the whole of the web, and it hasn&apos;t changed since 1992. This post follows one request through a Rails app: the address you type, the three files Rails reads to answer it, and the page that comes back.</p>
<h2 id="addresses-host-names-and-paths">Addresses, host names and paths</h2>
<p>The thing in your browser&apos;s address bar is a URL (Uniform Resource Locator). Every URL has the same parts, which is the &quot;uniform&quot;:</p>
<ul>
<li>the protocol, <code>https</code></li>
<li>the colon-slash-slash, <code>://</code></li>
<li>the host name, like <code>entr451.com</code></li>
<li>the path, like <code>/tacos</code></li>
</ul>
<p>The &quot;locator&quot; part means each address points at one thing, one resource. You&apos;d expect these two addresses to show a page about books and a page about shoes:</p>
<pre><code>https://YourDomainName.com/books
https://YourDomainName.com/shoes
</code></pre>
<p>While you build, the protocol is always <code>https</code>, and the host name is the long address your Codespace gives port 3000. So from here on we only talk about the path. When this post says <code>/tacos</code>, it means &quot;add <code>/tacos</code> to the end of the address in that browser tab.&quot;</p>
<h2 id="the-one-route-thats-already-there">The one route that&apos;s already there</h2>
<p>Open <code>config/routes.rb</code> in your copy of <code>web-apps</code>. Ignoring the comment, it has one line:</p>
<pre><code class="language-ruby">Rails.application.routes.draw do
  get(&quot;/data&quot;, { :controller =&gt; &quot;data&quot;, :action =&gt; &quot;show&quot; })
end
</code></pre>
<p>Read it as a sentence: when someone asks for <code>/data</code>, run the <code>show</code> action of the data controller. That line points at two more files:</p>
<ul>
<li><code>app/controllers/data_controller.rb</code>, a class with a method named <code>show</code>. The method decides what to show.</li>
<li><code>app/views/data/show.html.erb</code>, the page itself.</li>
</ul>
<p>Visit <code>/data</code> and you see every table in the database and its rows. That&apos;s three files in three folders, and every page in a Rails app is the same three: a route, a controller, a view. Now we&apos;ll write a page of our own: a page about tacos, naturally.</p>
<h2 id="dont-fear-the-error">Don&apos;t fear the error</h2>
<p><strong>The route.</strong> Add a line under the data line:</p>
<pre><code class="language-ruby">Rails.application.routes.draw do
  # get(&quot;/things&quot;, { :controller =&gt; &quot;things&quot;, :action =&gt; &quot;index&quot; })

  get(&quot;/data&quot;, { :controller =&gt; &quot;data&quot;, :action =&gt; &quot;show&quot; })
  get(&quot;/tacos&quot;, { :controller =&gt; &quot;tacos&quot;, :action =&gt; &quot;index&quot; })
end
</code></pre>
<p>Save, and visit <code>/tacos</code>. An error:</p>
<pre><code>uninitialized constant TacosController
</code></pre>
<p>Error pages in Rails are loud and scary. Don&apos;t be afraid of them. They&apos;re there to help, and they often say exactly what to do next, if you stop and read them. This one says that Rails read your route, went looking for a tacos controller, and didn&apos;t find one. So that&apos;s the next file.</p>
<p><strong>The controller.</strong> Rails has a command that makes it. In a terminal other than the one running the server, run it with the name from the route:</p>
<pre><code>bin/rails generate controller tacos
</code></pre>
<p>It makes two things: the file <code>app/controllers/tacos_controller.rb</code>, and an empty folder, <code>app/views/tacos/</code>, which is where the view will go. You could type the file yourself, but the generator puts the file and the folder exactly where Rails looks for them, named the way it expects. Open the file:</p>
<pre><code class="language-ruby">class TacosController &lt; ApplicationController
end
</code></pre>
<p>An empty class. Refresh, and Rails says what&apos;s missing:</p>
<pre><code>The action &apos;index&apos; could not be found for TacosController
</code></pre>
<p>The route asked for the <code>index</code> action, and an action is a method in the controller. Add it:</p>
<pre><code class="language-ruby">class TacosController &lt; ApplicationController
  def index
    render({ :template =&gt; &quot;tacos/index&quot; })
  end
end
</code></pre>
<p>The class name matches the route&apos;s <code>:controller</code>, and the method name matches its <code>:action</code>. The method has one job tonight: show the template <code>tacos/index</code>. Refresh. Another error, and we&apos;re still not afraid:</p>
<pre><code>Missing template tacos/index
</code></pre>
<p>Rails found the controller, ran <code>index</code>, and went looking for the view. The next file.</p>
<p><strong>The view.</strong> Make <code>app/views/tacos/index.html.erb</code>, in the folder the generator made. The folder matters: it&apos;s <code>app/views/tacos/</code>, not <code>app/views/</code>. Put some HTML in it:</p>
<pre><code class="language-html">&lt;h1&gt;Tacos&lt;/h1&gt;
&lt;p&gt;Every filling on the menu.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;carnitas&lt;/li&gt;
  &lt;li&gt;al pastor&lt;/li&gt;
  &lt;li&gt;barbacoa&lt;/li&gt;
&lt;/ul&gt;
</code></pre>
<p>Refresh, and there&apos;s the page. No more errors.</p>
<p>You didn&apos;t have to stop and start the server for any of this. Rails reads your files again on every request, so the next refresh always shows the latest save.</p>
<p>Each of those errors was Rails telling you where it got to. The route sends it to a controller, the controller runs an action, the action sends it to a view, and the error names the first piece that isn&apos;t there yet. That&apos;s worth remembering the next time a page doesn&apos;t work: refresh, read the error, make the piece it names.</p>
<h2 id="where-the-rest-of-the-html-came-from">Where the rest of the HTML came from</h2>
<p>Right-click the tacos page and choose View Page Source. You&apos;ll find something like this:</p>
<pre><code class="language-html">&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;web-apps&lt;/title&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width,initial-scale=1&quot;&gt;
    &lt;meta name=&quot;csrf-param&quot; content=&quot;authenticity_token&quot; /&gt;
    &lt;meta name=&quot;csrf-token&quot; content=&quot;...&quot; /&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;/assets/application-....css&quot; /&gt;
  &lt;/head&gt;

  &lt;body&gt;
    &lt;h1&gt;Tacos&lt;/h1&gt;
    &lt;p&gt;Every filling on the menu.&lt;/p&gt;
    &lt;ul&gt;
      &lt;li&gt;carnitas&lt;/li&gt;
      &lt;li&gt;al pastor&lt;/li&gt;
      &lt;li&gt;barbacoa&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p>Whoa. We only typed what&apos;s inside <code>&lt;body&gt;</code>. The rest comes from<br>
<code>app/views/layouts/application.html.erb</code>, the application layout. Every page in an app shares the same outer shell (the <code>DOCTYPE</code>, <code>&lt;html&gt;</code>, <code>&lt;head&gt;</code>), so Rails keeps it in one file and puts each view inside it, where the layout says <code>&lt;%= yield %&gt;</code>.</p>
<h2 id="whats-next">What&apos;s next</h2>
<p>That was a long way to get what a plain HTML file gives you. But every piece is now in place: an address, a route, a controller and a view. A view isn&apos;t only HTML, though. Its file name ends in <code>.erb</code>, and that&apos;s the next post: <a href="https://entr451.com/two-great-things/">Two great things</a>, HTML and Ruby in the same file.</p>
<p>Fell behind in class? <code>code-along-complete/1-tacos/</code> in your copy has all three files, at the same paths as yours.</p>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[Web Apps - Cards - Solution]]></title><description><![CDATA[<!--kg-card-begin: markdown--><h2 id="the-problem">The problem</h2>
<p>From the Practice section of the <code>web-apps</code> README:</p>
<blockquote>
<p><strong>Cards.</strong> Make a page at <code>/cards</code> that deals a five-card hand from a shuffled deck, a new hand every time the page loads. Build the deck from these two arrays:</p>
<pre><code>ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10,</code></pre></blockquote>]]></description><link>https://entr451.com/web-apps-cards-solution/</link><guid isPermaLink="false">6ae11876d9019e19441c352a</guid><dc:creator><![CDATA[Ben Block]]></dc:creator><pubDate>Tue, 27 Oct 2026 21:34:11 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><h2 id="the-problem">The problem</h2>
<p>From the Practice section of the <code>web-apps</code> README:</p>
<blockquote>
<p><strong>Cards.</strong> Make a page at <code>/cards</code> that deals a five-card hand from a shuffled deck, a new hand every time the page loads. Build the deck from these two arrays:</p>
<pre><code>ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, &quot;Jack&quot;, &quot;Queen&quot;, &quot;King&quot;, &quot;Ace&quot;]
suits = [&quot;Clubs&quot;, &quot;Diamonds&quot;, &quot;Hearts&quot;, &quot;Spades&quot;]
</code></pre>
<p>Show each card&apos;s name (&quot;Queen of Hearts&quot;). Then show its picture: <code>public/images/cards/</code> has one per card, named like <code>queen_of_hearts.svg</code>, and the page can show it with <code>&lt;img src=&quot;/images/cards/queen_of_hearts.svg&quot;&gt;</code>. Add a link that deals again.</p>
</blockquote>
<p>You&apos;ve dealt this hand before, in lab 5 of <code>ruby-intro</code>. That time it went to the terminal with <code>puts</code>; this time it goes on a page.</p>
<h2 id="1-the-route-and-the-controller">1. The route and the controller</h2>
<pre><code class="language-ruby">get(&quot;/cards&quot;, { :controller =&gt; &quot;cards&quot;, :action =&gt; &quot;index&quot; })
</code></pre>
<pre><code>bin/rails generate controller cards
</code></pre>
<pre><code class="language-ruby">class CardsController &lt; ApplicationController
  def index
    render({ :template =&gt; &quot;cards/index&quot; })
  end
end
</code></pre>
<h2 id="2-the-deck">2. The deck</h2>
<p>A loop inside a loop: for every rank, every suit. That&apos;s 13 times 4, 52 cards. Start with an empty array and push each card onto it:</p>
<pre><code class="language-erb">&lt;% ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, &quot;Jack&quot;, &quot;Queen&quot;, &quot;King&quot;, &quot;Ace&quot;] %&gt;
&lt;% suits = [&quot;Clubs&quot;, &quot;Diamonds&quot;, &quot;Hearts&quot;, &quot;Spades&quot;] %&gt;
&lt;% deck = [] %&gt;
&lt;% for rank in ranks %&gt;
  &lt;% for suit in suits %&gt;
    &lt;% card = { &quot;name&quot; =&gt; &quot;#{rank} of #{suit}&quot;, &quot;image&quot; =&gt; &quot;#{rank}_of_#{suit}&quot;.downcase } %&gt;
    &lt;% deck.push(card) %&gt;
  &lt;% end %&gt;
&lt;% end %&gt;
</code></pre>
<p>Each card needs two things: a name to show (&quot;Queen of Hearts&quot;) and the name of its picture (<code>queen_of_hearts</code>). They differ in two ways, the spaces and the capital letters, so a hash holds both, the way a row from the database holds its columns. <code>card[&quot;name&quot;]</code> and <code>card[&quot;image&quot;]</code> read them the way <code>company[&quot;name&quot;]</code> did.</p>
<p><code>&quot;#{rank}_of_#{suit}&quot;</code> is &quot;Queen_of_Hearts&quot;; <code>.downcase</code> makes it &quot;queen_of_hearts&quot;, the file&apos;s name. A number works too: <code>&quot;#{10}_of_Clubs&quot;.downcase</code> is &quot;10_of_clubs&quot;.</p>
<h2 id="3-shuffle-then-deal-five">3. Shuffle, then deal five</h2>
<pre><code class="language-erb">&lt;% hand = deck.shuffle[0, 5] %&gt;
</code></pre>
<p><code>shuffle</code> gives back the deck in a random order. <code>[0, 5]</code> takes five cards, starting from the first. Every refresh is a new request, so the view runs again and the deck is shuffled again.</p>
<p>This is why the hand comes from a shuffled deck, instead of picking five cards with <code>rand</code> the way the dice did: a deck has one Queen of Hearts. Five <code>rand</code> picks could pick the same card twice; five cards off the top of a shuffled deck can&apos;t. Two dice can match. Two cards in one hand can&apos;t.</p>
<h2 id="4-the-hand-on-the-page">4. The hand on the page</h2>
<pre><code class="language-erb">&lt;h1&gt;Cards&lt;/h1&gt;
&lt;p&gt;
  &lt;% for card in hand %&gt;
    &lt;img src=&quot;/images/cards/&lt;%= card[&quot;image&quot;] %&gt;.svg&quot; width=&quot;100&quot; alt=&quot;&lt;%= card[&quot;name&quot;] %&gt;&quot;&gt;
  &lt;% end %&gt;
&lt;/p&gt;
&lt;ul&gt;
  &lt;% for card in hand %&gt;
    &lt;li&gt;&lt;%= card[&quot;name&quot;] %&gt;&lt;/li&gt;
  &lt;% end %&gt;
&lt;/ul&gt;
&lt;a href=&quot;/cards&quot;&gt;Deal again&lt;/a&gt;
</code></pre>
<p>The <code>alt</code> text is what a screen reader says for the picture, and what the browser shows if the picture can&apos;t load. The card&apos;s name is exactly right for it.</p>
<h2 id="the-whole-view">The whole view</h2>
<pre><code class="language-erb">&lt;% ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, &quot;Jack&quot;, &quot;Queen&quot;, &quot;King&quot;, &quot;Ace&quot;] %&gt;
&lt;% suits = [&quot;Clubs&quot;, &quot;Diamonds&quot;, &quot;Hearts&quot;, &quot;Spades&quot;] %&gt;
&lt;% deck = [] %&gt;
&lt;% for rank in ranks %&gt;
  &lt;% for suit in suits %&gt;
    &lt;% card = { &quot;name&quot; =&gt; &quot;#{rank} of #{suit}&quot;, &quot;image&quot; =&gt; &quot;#{rank}_of_#{suit}&quot;.downcase } %&gt;
    &lt;% deck.push(card) %&gt;
  &lt;% end %&gt;
&lt;% end %&gt;
&lt;% hand = deck.shuffle[0, 5] %&gt;
&lt;h1&gt;Cards&lt;/h1&gt;
&lt;p&gt;
  &lt;% for card in hand %&gt;
    &lt;img src=&quot;/images/cards/&lt;%= card[&quot;image&quot;] %&gt;.svg&quot; width=&quot;100&quot; alt=&quot;&lt;%= card[&quot;name&quot;] %&gt;&quot;&gt;
  &lt;% end %&gt;
&lt;/p&gt;
&lt;ul&gt;
  &lt;% for card in hand %&gt;
    &lt;li&gt;&lt;%= card[&quot;name&quot;] %&gt;&lt;/li&gt;
  &lt;% end %&gt;
&lt;/ul&gt;
&lt;a href=&quot;/cards&quot;&gt;Deal again&lt;/a&gt;
</code></pre>
<p>That&apos;s a lot of Ruby for a view. It works, and for one page it&apos;s fine. Next week, Ruby that works something out moves into the controller, and the view goes back to mostly showing it.</p>
<h2 id="when-it-went-wrong">When it went wrong</h2>
<ul>
<li><strong>Broken images.</strong> The file name: lowercase, underscores, <code>.svg</code> on the end. View the page source and compare one <code>src</code> with a file name in <code>public/images/cards/</code>.</li>
<li><strong>The same card twice in a hand.</strong> Five <code>rand</code> picks instead of one shuffled deck.</li>
<li><strong>52 cards on the page.</strong> The loop over <code>deck</code> instead of <code>hand</code>.</li>
<li><strong>&quot;Queen of Hearts&quot; in the image name.</strong> The name used where the image belongs: spaces and capitals, so no file matches.</li>
</ul>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[Web Apps - Contacts - Solution]]></title><description><![CDATA[<!--kg-card-begin: markdown--><h2 id="the-problem">The problem</h2>
<p>From the Practice section of the <code>web-apps</code> README:</p>
<blockquote>
<p><strong>Contacts.</strong> Make a page at <code>/contacts</code> that lists every contact&apos;s first and last name and the name of their company. Look the company up with <code>Company.find_by({ &quot;id&quot; =&gt; contact[&quot;company_id&quot;] })</code>. Then</p></blockquote>]]></description><link>https://entr451.com/web-apps-contacts-solution/</link><guid isPermaLink="false">6ae117ebd9019e19441c3513</guid><dc:creator><![CDATA[Ben Block]]></dc:creator><pubDate>Tue, 27 Oct 2026 21:32:01 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><h2 id="the-problem">The problem</h2>
<p>From the Practice section of the <code>web-apps</code> README:</p>
<blockquote>
<p><strong>Contacts.</strong> Make a page at <code>/contacts</code> that lists every contact&apos;s first and last name and the name of their company. Look the company up with <code>Company.find_by({ &quot;id&quot; =&gt; contact[&quot;company_id&quot;] })</code>. Then add each contact&apos;s email.</p>
</blockquote>
<p>It&apos;s the companies page from class, with one new thing: a contact doesn&apos;t hold its company&apos;s name, only its company&apos;s <code>id</code>.</p>
<h2 id="1-the-route-and-the-controller">1. The route and the controller</h2>
<p>In <code>config/routes.rb</code>, under the companies line:</p>
<pre><code class="language-ruby">get(&quot;/contacts&quot;, { :controller =&gt; &quot;contacts&quot;, :action =&gt; &quot;index&quot; })
</code></pre>
<p>In the terminal that isn&apos;t running the server:</p>
<pre><code>bin/rails generate controller contacts
</code></pre>
<p>And the action, in <code>app/controllers/contacts_controller.rb</code>:</p>
<pre><code class="language-ruby">class ContactsController &lt; ApplicationController
  def index
    render({ :template =&gt; &quot;contacts/index&quot; })
  end
end
</code></pre>
<p>The errors on the way are the tacos ones: no controller, unknown action, missing template.</p>
<h2 id="2-every-contacts-name">2. Every contact&apos;s name</h2>
<p><code>app/views/contacts/index.html.erb</code>. The same loop as the companies page, over <code>Contact.all</code>:</p>
<pre><code class="language-erb">&lt;h1&gt;Contacts&lt;/h1&gt;
&lt;ul&gt;
  &lt;% for contact in Contact.all %&gt;
    &lt;li&gt;&lt;%= contact[&quot;first_name&quot;] %&gt; &lt;%= contact[&quot;last_name&quot;] %&gt;&lt;/li&gt;
  &lt;% end %&gt;
&lt;/ul&gt;
</code></pre>
<p>Four names: Tim Cook, Craig Federighi, Andy Jassy, Dario Amodei.</p>
<h2 id="3-the-company-by-its-id">3. The company, by its id</h2>
<p>Open <code>/data</code> and look at the contacts table. Tim Cook&apos;s <code>company_id</code> is 1, and the company whose <code>id</code> is 1 is Apple. So for each contact, find the company whose <code>id</code> is that contact&apos;s <code>company_id</code>:</p>
<pre><code class="language-erb">&lt;% company = Company.find_by({ &quot;id&quot; =&gt; contact[&quot;company_id&quot;] }) %&gt;
</code></pre>
<p><code>find_by</code> gives back one row, the first that matches, so <code>company[&quot;name&quot;]</code> is its name. It goes inside the loop, because each contact has its own company:</p>
<pre><code class="language-erb">&lt;h1&gt;Contacts&lt;/h1&gt;
&lt;ul&gt;
  &lt;% for contact in Contact.all %&gt;
    &lt;% company = Company.find_by({ &quot;id&quot; =&gt; contact[&quot;company_id&quot;] }) %&gt;
    &lt;li&gt;
      &lt;%= contact[&quot;first_name&quot;] %&gt; &lt;%= contact[&quot;last_name&quot;] %&gt;, &lt;%= company[&quot;name&quot;] %&gt;, &lt;%= contact[&quot;email&quot;] %&gt;
    &lt;/li&gt;
  &lt;% end %&gt;
&lt;/ul&gt;
</code></pre>
<p>The email was the last step: <code>contact[&quot;email&quot;]</code>, one more cell of the same row.</p>
<pre><code>Tim Cook, Apple, tim@apple.com
Craig Federighi, Apple, craig@apple.com
Andy Jassy, Amazon, andy@amazon.com
Dario Amodei, Anthropic, dario@anthropic.com
</code></pre>
<h2 id="what-the-log-shows">What the log shows</h2>
<p>Refresh the page and look at the bottom of <code>log/development.log</code>. One <code>SELECT</code> for the contacts, then one more for each contact&apos;s company, with a <code>WHERE &quot;companies&quot;.&quot;id&quot; = ?</code> in it: five lines in all. The second Apple one starts with <code>CACHE</code>: Craig&apos;s company is Tim&apos;s, and Rails remembered the answer from a moment before, inside the same request. Every other <code>find_by</code> is its own trip to the database. For four contacts that&apos;s nothing. For four thousand it would be slow, and later in the course you&apos;ll see ways to ask for all of it at once. For now, the point is that you can read exactly what your page asked the database for.</p>
<h2 id="when-it-went-wrong">When it went wrong</h2>
<ul>
<li><strong>undefined method &apos;[]&apos; for nil.</strong> <code>find_by</code> found nothing, so <code>company</code> is <code>nil</code>. Usually a typo in the column: <code>&quot;company_id&quot;</code>, with the underscore. (Or a contact whose company was deleted.)</li>
<li><strong>Every contact shows the same company.</strong> The <code>find_by</code> line is above the loop instead of inside it.</li>
<li><strong>company.name instead of company[&quot;name&quot;].</strong> It works, but stay with the brackets: a row is read the same way everywhere in this course, column name in brackets.</li>
</ul>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[Web Apps - Dice - Solution]]></title><description><![CDATA[<!--kg-card-begin: markdown--><h2 id="the-problem">The problem</h2>
<p>From class:</p>
<blockquote>
<ol>
<li>A route for <code>/dice</code> in <code>config/routes.rb</code>.</li>
<li><code>bin/rails generate controller dice</code>, then an <code>index</code> action in it.</li>
<li>An index view in <code>app/views/dice/</code>.</li>
<li>In the view, roll two dice. Show both, and the total.</li>
</ol>
<p>One die: <code>rand(1..6)</code>. Challenge: the pictures, <code>&lt;</code></p></blockquote>]]></description><link>https://entr451.com/web-apps-dice-solution/</link><guid isPermaLink="false">6ae116eed9019e19441c34ec</guid><dc:creator><![CDATA[Ben Block]]></dc:creator><pubDate>Tue, 27 Oct 2026 21:29:43 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><h2 id="the-problem">The problem</h2>
<p>From class:</p>
<blockquote>
<ol>
<li>A route for <code>/dice</code> in <code>config/routes.rb</code>.</li>
<li><code>bin/rails generate controller dice</code>, then an <code>index</code> action in it.</li>
<li>An index view in <code>app/views/dice/</code>.</li>
<li>In the view, roll two dice. Show both, and the total.</li>
</ol>
<p>One die: <code>rand(1..6)</code>. Challenge: the pictures, <code>&lt;img src=&quot;/images/dice/1.svg&quot;&gt;</code>. <code>1.svg</code> to <code>6.svg</code> are already in <code>public/images/dice/</code>.</p>
<p>Done? Check your page against your three sentences.</p>
</blockquote>
<p>Before you built it, you wrote three sentences that would tell you the page works. Keep them next to you: the last section checks the page against them.</p>
<p>This is the tacos page again, with Ruby doing more of the work. Same three files, same errors, in the same order.</p>
<h2 id="1-the-route">1. The route</h2>
<p>In <code>config/routes.rb</code>, under the tacos line:</p>
<pre><code class="language-ruby">get(&quot;/dice&quot;, { :controller =&gt; &quot;dice&quot;, :action =&gt; &quot;index&quot; })
</code></pre>
<p>Visit <code>/dice</code>: &quot;uninitialized constant DiceController&quot;. Rails read the route and went looking for a controller called dice.</p>
<h2 id="2-the-controller">2. The controller</h2>
<p>In the terminal that isn&apos;t running the server, with the name from the route:</p>
<pre><code>bin/rails generate controller dice
</code></pre>
<p>It makes <code>app/controllers/dice_controller.rb</code>, an empty class, and the empty folder <code>app/views/dice/</code>. Refresh: &quot;The action &apos;index&apos; could not be found for DiceController&quot;. The route asked for <code>index</code>, and an action is a method. Add it:</p>
<pre><code class="language-ruby">class DiceController &lt; ApplicationController
  def index
    render({ :template =&gt; &quot;dice/index&quot; })
  end
end
</code></pre>
<p>Refresh: &quot;Missing template dice/index&quot;. One file to go.</p>
<h2 id="3-the-view-with-numbers-first">3. The view, with numbers first</h2>
<p>Make <code>app/views/dice/index.html.erb</code>, in the folder the generator made. Roll each die once, at the top, and keep the result in a variable:</p>
<pre><code class="language-erb">&lt;% die1 = rand(1..6) %&gt;
&lt;% die2 = rand(1..6) %&gt;
&lt;h1&gt;Dice&lt;/h1&gt;
&lt;p&gt;
  You rolled &lt;%= die1 %&gt; and &lt;%= die2 %&gt;, for a total of &lt;%= die1 + die2 %&gt;.
&lt;/p&gt;
</code></pre>
<p><code>rand(1..6)</code> is a whole number from 1 to 6, picked at random. The variables matter: if you wrote <code>rand(1..6)</code> straight into the sentence, the total would come from two new rolls, not the two on the page. Roll once, then use the variables everywhere.</p>
<p><code>&lt;% %&gt;</code> runs the Ruby and shows nothing; <code>&lt;%= %&gt;</code> runs it and puts the result on the page.</p>
<p>Refresh a few times. Every refresh is a new request, so the view runs again from the top and rolls again.</p>
<h2 id="4-the-pictures">4. The pictures</h2>
<p>The images are named for their number, <code>1.svg</code> to <code>6.svg</code>. So the number goes inside the file name:</p>
<pre><code class="language-erb">&lt;p&gt;
  &lt;img src=&quot;/images/dice/&lt;%= die1 %&gt;.svg&quot; width=&quot;100&quot;&gt;
  &lt;img src=&quot;/images/dice/&lt;%= die2 %&gt;.svg&quot; width=&quot;100&quot;&gt;
&lt;/p&gt;
</code></pre>
<p>When <code>die1</code> is 4, Rails writes <code>&lt;img src=&quot;/images/dice/4.svg&quot;&gt;</code>, and the browser asks for that picture. The path starts with <code>/images</code>, not <code>/public/images</code>: everything in <code>public/</code> is served from the top of the site.</p>
<h2 id="5-roll-again">5. Roll again</h2>
<p>A link back to the same page asks for it again, which rolls again:</p>
<pre><code class="language-erb">&lt;a href=&quot;/dice&quot;&gt;Roll again&lt;/a&gt;
</code></pre>
<h2 id="the-whole-view">The whole view</h2>
<pre><code class="language-erb">&lt;% die1 = rand(1..6) %&gt;
&lt;% die2 = rand(1..6) %&gt;
&lt;h1&gt;Dice&lt;/h1&gt;
&lt;p&gt;
  &lt;img src=&quot;/images/dice/&lt;%= die1 %&gt;.svg&quot; width=&quot;100&quot;&gt;
  &lt;img src=&quot;/images/dice/&lt;%= die2 %&gt;.svg&quot; width=&quot;100&quot;&gt;
&lt;/p&gt;
&lt;p&gt;
  You rolled &lt;%= die1 %&gt; and &lt;%= die2 %&gt;, for a total of &lt;%= die1 + die2 %&gt;.
&lt;/p&gt;
&lt;a href=&quot;/dice&quot;&gt;Roll again&lt;/a&gt;
</code></pre>
<h2 id="check-it-against-your-sentences">Check it against your sentences</h2>
<p>Three that the room came up with, and how to check each one by looking at the page:</p>
<ul>
<li><strong>Each die shows a number from 1 to 6.</strong> Refresh ten times. You should never see a 0 or a 7.</li>
<li><strong>The total is the sum of the two dice.</strong> Add them in your head, every refresh.</li>
<li><strong>A link rolls again.</strong> Click it; the dice change (most of the time: two rolls can match).</li>
</ul>
<p>If one of yours was &quot;it looks good&quot; or &quot;it&apos;s random&quot;, try rewording it into something you could count or compare. &quot;It&apos;s random&quot; is hard to check by looking once; &quot;the dice aren&apos;t the same every time I refresh&quot; is easy.</p>
<h2 id="when-it-went-wrong">When it went wrong</h2>
<ul>
<li><strong>The same roll every time.</strong> A number typed where <code>rand(1..6)</code> belongs.</li>
<li><strong>A broken image.</strong> The path: <code>/images/dice/3.svg</code>, with the slash at the front and no <code>public/</code>.</li>
<li><strong>The total doesn&apos;t match the dice.</strong> <code>rand(1..6)</code> written into the sentence instead of the variables, so the total rolled its own dice.</li>
<li><strong>The generator did nothing.</strong> It was typed into the terminal running the server, which isn&apos;t taking commands. Use the other one.</li>
</ul>
<p>For a slower walk through the same page, with the tacos page before it, see<br>
<a href="https://entr451.com/two-great-things/">Two Great Things</a>.</p>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[Adding a column to a table]]></title><description><![CDATA[<!--kg-card-begin: markdown--><blockquote>
<p><a href="https://entr451.com/migrations/">Migrations</a> showed how a table is born: <code>bin/rails generate model</code>, columns inside <code>create_table</code>, then <code>bin/rails db:migrate</code>. This is the next thing that happens to every table: a column it needs that wasn&apos;t there on day one.</p>
</blockquote>
<h2 id="the-problem">The problem</h2>
<p>Your <code>movies</code> table is made and</p>]]></description><link>https://entr451.com/adding-a-column/</link><guid isPermaLink="false">6ad8f72ad9019e19441c34cb</guid><dc:creator><![CDATA[Ben Block]]></dc:creator><pubDate>Wed, 21 Oct 2026 17:34:16 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><blockquote>
<p><a href="https://entr451.com/migrations/">Migrations</a> showed how a table is born: <code>bin/rails generate model</code>, columns inside <code>create_table</code>, then <code>bin/rails db:migrate</code>. This is the next thing that happens to every table: a column it needs that wasn&apos;t there on day one.</p>
</blockquote>
<h2 id="the-problem">The problem</h2>
<p>Your <code>movies</code> table is made and migrated, and your tests are green. Now a new story says every movie has a director, and there is no column for it.</p>
<p>The migration that created the table has already run, and Rails won&apos;t run it again. Editing it changes nothing in the database, and it leaves a file that lies about what happened. So the rule from <a href="https://entr451.com/migrations/">Migrations</a> still holds: never edit a migration you have run.</p>
<p>The fix is not to start over. It is one more migration, one that changes the table instead of creating it.</p>
<h2 id="the-new-migration">The new migration</h2>
<p>Ask Rails for an empty migration. The name says what it does; Rails only needs it to be unique.</p>
<pre><code>bin/rails generate migration AddDirectorToMovies
</code></pre>
<p>That makes a new file in <code>db/migrate/</code>, with a newer timestamp than the one that created the table, so it runs after it. Open it. Its <code>change</code> method is empty. Put one line in it:</p>
<pre><code class="language-ruby">class AddDirectorToMovies &lt; ActiveRecord::Migration[8.1]
  def change
    add_column :movies, :director, :string
  end
end
</code></pre>
<p><code>add_column</code> takes the table, the new column and its type: the same types as before, <code>:string</code> for text and <code>:integer</code> for numbers and ids. Then run it:</p>
<pre><code>bin/rails db:migrate
</code></pre>
<p>Open <code>db/schema.rb</code>. The <code>movies</code> table now lists <code>director</code>. You didn&apos;t write that line; Rails did, from the migration. The rows you already had are still there, each with an empty director until something fills it in.</p>
<h2 id="what-it-does-to-your-tests">What it does to your tests</h2>
<p>Nothing, until a rule says the column matters. The moment your definition of done says &quot;a movie can&apos;t be saved without a director&quot; and the model gets that rule, the tests you already had start to react. Any test that saved a movie on its way to checking something else is now saving one with no director, and what happens next depends on what that test was checking. Read each failure for what the test was relying on, and ask whether any test is now passing for a reason other than its own rule. That is not the tests breaking; that is the tests doing their job. Each of those movies now has to be given a director, the way the real app would. When you ask your agent for the new tests, expect it to touch the old ones too, and read what it changed.</p>
<h2 id="the-other-direction">The other direction</h2>
<p>A column that should go away is the same shape, with <code>remove_column</code>:</p>
<pre><code class="language-ruby">remove_column :movies, :director, :string
</code></pre>
<p>A new migration, never an edit to an old one. The database&apos;s history is the list of files in <code>db/migrate/</code>, in order, and <code>db/schema.rb</code> shows the database&apos;s state up to the most recently executed (i.e. ran) migration.</p>
<!--kg-card-end: markdown-->]]></content:encoded></item></channel></rss>