I Suddenly Know How It All Fits Together Better

lego stormrooper on a skateboard

I moved through a lot of curriculum today. However, I really want to hit the goal I set two weeks ago and I’m on the cusp of it! I had 21 lessons/labs to go for my goal when I checked early in the day. So I set a goal of 11 more today (I had already finished 2) and finish tomorrow. I missed it by one only because it’s late and I hit an hour and a half review lecture. Since I’m working on ActiveRecord sessions and authentication I want to pay better attention to this lecture than I usually do. Maybe even take a note or two :-).

Ealy in the day I got stuck on a nested forms lab. I was getting an error:

Failures:
  1) App POST /teams submits the form
     Failure/Error: click_button 'submit'
     TypeError:
       expected Hash (got String) for param `name'

but I couldn’t figure out why. My code was:

<h1>Create a Team and Heroes!</h1>
<form action="/teams" method="POST">
  <p>Team Name: <input type="text" name="team[name]"></p>
  <p>Team Motto: <input type="text" name="team[motto]"></p>
  <h2>Hero 1</h2>
  <p>Hero's Name: <input id="member1_name" type="text" name="team[name][hero][][name]"></p>
  <p>Hero's Power: <input id="member1_power" type="text" name="team[name][hero][][power]"></p>
  <p>Hero's Biography: <input id="member1_bio" type="text" name="team[name][hero][][bio]"></p>
  <h2>Hero 2</h2>
  <p>Hero's Name: <input id="member2_name" type="text" name="team[name][hero][][name]"></p>
  <p>Hero's Power: <input id="member2_power" type="text" name="team[name][hero][][power]"></p>
  <p>Hero's Biography: <input id="member2_bio" type="text" name="team[name][hero][][bio]"></p>
  <h2>Hero 3</h2>
  <p>Hero's Name: <input id="member3_name" type="text" name="team[name][hero][][name]"></p>
  <p>Hero's Power: <input id="member3_power" type="text" name="team[name][hero][][power]"></p>
  <p>Hero's Biography: <input id="member3_bio" type="text" name="team[name][hero][][bio]"></p>
  <button type="submit" value="submit">Submit</button>
</form>

I kept thinking, “How can form input be anything other than a String? Why is it expecting a hash?” I reached out to a Learn Expert (they’re like Teaching Assistants who are available almost 24/7 to help) and it took almost an hour to resolve it. However, figured out that because my third line had name="team[name] whenever I called a nested hash that contained team[name] it was returning the string I input instead of setting the params nested hash array to the proper thing. A quick change of my code to:

<h1>Create a Team and Heroes!</h1>
<form action="/teams" method="POST">
  <p>Team Name: <input type="text" name="team[name]"></p>
  <p>Team Motto: <input type="text" name="team[motto]"></p>
  <h2>Hero 1</h2>
  <p>Hero's Name: <input id="member1_name" type="text" name="team[hero][][name]"></p>
  <p>Hero's Power: <input id="member1_power" type="text" name="team[hero][][power]"></p>
  <p>Hero's Biography: <input id="member1_bio" type="text" name="team[hero][][bio]"></p>
  <h2>Hero 2</h2>
  <p>Hero's Name: <input id="member2_name" type="text" name="team[hero][][name]"></p>
  <p>Hero's Power: <input id="member2_power" type="text" name="team[hero][][power]"></p>
  <p>Hero's Biography: <input id="member2_bio" type="text" name="team[hero][][bio]"></p>
  <h2>Hero 3</h2>
  <p>Hero's Name: <input id="member3_name" type="text" name="team[hero][][name]"></p>
  <p>Hero's Power: <input id="member3_power" type="text" name="team[hero][][power]"></p>
  <p>Hero's Biography: <input id="member3_bio" type="text" name="team[hero][][bio]"></p>
  <button type="submit" value="submit">Submit</button>
</form>

and my tests were all passing.

I also realized today that I’ve been achieving the end goal of clearing out hashes without using the method provided by Ruby. When I read:

Luckily for us, there is already a Ruby method for emptying a hash: #clear.

I thought to myself, “Whoops!” because I’ve just been resetting hashes & arrays by doing @session = {} or @@all = [] up until this point. I haven’t checked the docs yet to make sure this method applies to arrays as well but I feel like I’ve seen it used so I’m relatively confident in that. I guess I’ll find out the first time I use it.

Time spent today: 6:37
Time spent total: 165:06
Lessons completed today: 12
Lessons completed total: 392