Forms forms forms: App Academy w4d5
Yesterday (Friday) we made another rails app (!!). We made it farther along in the process, partly with the increased speed that comes from repetition, and partly because we skipped some some less interesting requirements.
Much of our time was spent making forms, which are kind of a giant pain in the ass. If I want to make a single text entry box for "band name: ", I must write "band name" four--sometimes five--different times, in three different permutations, in the HTML that generates that field. Here's an example:
<label for="band_name">Band name</label> <input type="text" name="band[name]" id="band_name" value "<%=value(band, :name)%>" >'
Don't miss any quotations marks or curly braces! The server will get mad if you do.
Let me dissect the above code:
<label --Okay, we're making a label for a field.
for= --Which field? The functionality of this portion seems not strictly necessary, but we've been instructed to always do it for two great reasons. The first is for accessibility: content readers for the blind rely on fields being labeled in this way. The second is that apparently if the user clicks on the label name, their cursor will be placed in the input field for that label. This is especially important in mobile environments, or, only works in mobile environments (I wasn't clear from the lecture).
"band_name"> --this is what the label's for. Must exactly match what comes after id= in the input type= section below. The angle bracket indicates that it's part of
Band name --this is what the user will see as the label for the input field. Notice it's not a string. If it had quotation mark, the user would see those too.
</label> closes out the label element.
<input type="text" --We're making a text input field. This is a single-line field for the user to enter text into (if we wanted a multi-line box, we'd use <textarea name= ).
name="band[name]" -- this tells rails where to store the string that the user submits. In this case, params[band][name]. params is a magical multi-layered hash, and band is a key within it. The band key points to a hash, and name is a key in it.
id="band_name" -- Remember that for="band_name" from the label earlier? This is the part of the field that's matched up with the that part of the label. The two are associated now.
In order to understand the value part of the HTML, I must first do more reading. Back in a bit.
Okay, I'm back. And I'm ready to collect a prize as the first person on the internet to return to a post as promised. I know you're on the edge of your seat, so without further ado, here's the value section, explained:
value"<%=value(band, :name)%>" >' This snippet will return the band name if it exists, and nothing if it doesn't. Because this field comes from a form that can be used to create or edit, we want the ability to have a field be pre-defined or not. In other words, if you go to edit a band's info, you want to start with the current info waiting in the field for you to edit. This code puts it there.