Wednesday, June 1, 2011

A little one-on-one with jqr

So last night my friend Eli calls me up, interrupting my special video game time and tells me to get my ass over to his house for a little side-by-side code action.

You want to talk about productive? In about an hour, Eli had shown me ways to solve both of the problems I've been having with Chorenivore and Manticore (I promise not all of my projects will end in an -ore sound) as well as digging into some more abstract theory stuff.

Basically what I've got now is a hidden_field and a couple of image_submit_tags that mark a task as finished or unfinished. We grabbed a couple of placeholder images, just so I could see how it worked and fired it up. BOOM! Marking a task as Finished displayed a nice little check mark, whereas marking a task as Unfinished displayed a little boxy box.

Question:

Here's the code for my hidden field in the Task view:

<%= f.hidden_field :finished, :value => !task.finished %>


From this syntax, my understanding is the hidden field is marking the task as finished or unfinished without being displayed to the user. Is this correct? How else is a hidden_field used?

After work, I decided I'd fire up Illustrator and Photoshop and see if I couldn't make a couple nicer looking little icons. Here's what I came up with at first:


What? Are you drunk? Look, I like big icons as much as the next guy, but 80 x 80 is just egregious. Let's shrink that down a little, son.


Aha! Now it's starting to come together. Plus, to use these images, all I had to do was throw them in the public/images folder, then call either 'finished.png' or 'unfinished.png'.

Not bad for Chorenivore!

Here's a little recap of the problem I ran into with Manticore a while back. David had shown me how to create instances of related models from my Character view that are tied to my main Character model. But once I got everything straightened out that I want displayed in the Character view (hit points, combat information, statistics, saves) I hit a wall. I wanted a way to link to another page within the same application that would display different information. For example, every Character will have Items. Items will be broken down into Weapons, Armor, Magical Items, Gear and Treasure.

The solution was this:

<%= link_to_unless_current 'Armor', character_armor_path(@character, @character.armor) %>


See, I KNEW I could do something like this, but the exact syntax was giving me trouble. But now that I know how to link to the view of a model that belongs_to another model, I can design the navigation fairly easily. You just throw that bad boy into a partial and call it macaroni!

I know working on functionality before worrying about style is the way to go, but I still have a hard time not thinking about how I want an application to be navigated.

So what did I take away from this, besides concrete Rails techniques? The internet is a great and valuable resource, but actually hanging out with and talking to people about projects is irreplaceable. Not only did Eli show me how to do some things I've been struggling with for about a month, but I also talked to all the Fastest Forward guys about projects they're working on. It's awesome to see people excited about what they're doing.

2 comments:

  1. The thing to remember when designing forms is that you're essentially constructing a hash for use by your controller. Say you have a form for user login:

    <%= form_for :user ... do |f| %>
    <%= f.text_field :name %>
    ...

    This will produce input HTML like:

    input id="user_name" name="user[name]" type="text"

    When submitted, this will result in params like:

    { ..., "user"=>{"name"=>"matt", ...} }

    Notice how form helper parameters correspond to input id and name, and how these correspond to the params hash.

    As for hidden fields, they're just a way to include values in the params hash without displaying them to the user.

    ReplyDelete
  2. So another way of thinking about a form is it's a way for a user to interact with the database? I knew that's what they did, but I wasn't thinking about them in quite this way until now.

    ReplyDelete