Showing posts with label forms. Show all posts
Showing posts with label forms. Show all posts

Tuesday, December 13, 2011

Trouble with Ruby form methods

I'm still working with my Dungeon Crawler Sinatra app. Believe it or not, I have a problem. I'm trying to get a simple form method working to allow a user to move from one room to another by typing in the direction he wants to move and then hitting 'Submit'. So good so far, right?

Here's a link to a gist of the code I've got so far:

Working with form methods

The second file I posted in the gist is code that works, but I'm looking for a cleaner way of manipulating the information collected from the form method. Does that make sense? This is probably easy enough to do, and @jqr gave me some help with it last week, but I can't figure out how to make these form methods behave.

On a side note, do you have a problem with getting distracted with new ideas for projects when you're in the middle of a project? This seems to happen to me all the time, and I've been handling it by saving as much as I can think of in a Google Doc. I wonder how other programmers handle this? Do you just write things down in a notebook? I've been thinking about creating a template to use for these project ideas, just to keep my thoughts a bit more organized. If any of you have a template like this, or suggestions on what would be key features, I'd love to hear them.

I actually think a simple drawing application that allows me to add notes and save to the cloud would be ideal, but I haven't heard of anything like this.

Saturday, May 21, 2011

A little late-night coding

I wish I didn't have to get up at 7 am during the week. I've always felt most productive and focused when working on projects late at night.

I've been reading the archives at railstips.org lately. I'm using some liberty when I say "reading the archives," because I really only got to this article on good Rails tutorials.

On a side note: I've linked to a 5 year old article which is a link to another article which is a collection of Rails tutorials. Is that ridiculous?

ANYWAY! My friend Eli encouraged me to try out some of these tutorials, despite their age. Most of them use bizarre, antiquated syntax. Luckily, I found an updated version of the first tutorial, which walks you through creating a recipe database. It's a simple application, allowing a user to create recipes and categories, then assign recipes to categories. I can think of several ways to expand functionality for this application: allowing users to search by ingredients, sorting recipes by categories, allowing users to create meals consisting of multiple recipes and so on.

I try to think of these things when I'm doing a tutorial or working on a personal project. I always ask "What else could be done? Where else could this go?" Luckily, Rails not only makes it easy to build something functional very quickly, it also makes adding functionality easy. I'm not sure if this is true for most other languages, but I highly doubt I'd have gotten interested in Rails if it wasn't so simple to pick up the basics.

Not that I think I'm an expert, or even knowledgable! I still get discouraged, especially when I can't figure out how to do something that SHOULD be easy, but I'm getting better every day. I'm planning to work through some of the tutorials on that list, hoping to learn some techniques that I can apply to Manticore and other projects.

A couple of interesting things I learned tonight:

Linking back to the main page of an application:

I got on irc and asked about this. It turns out the trick is this:

<%= link_to 'Home', :root %>

I'd tried linking to various forms of home, index, homes_path and index_path with no luck. This nice little trick saves a bit of time when navigating, though in a real application I'd want to use breadcrumbs instead. I'm assuming that would be built in much the same way?

Pulling information from one model and displaying it in the form of a second model:

In this application, I have two models: Recipe and Category. Each Recipe will belong to a Category, and to build this information in the form for a new Recipe, here's the code:

<div class="field">
  <%= f.label :category %><br />
  <%= select "recipe", "category_id", Category.find(:all).collect {|p| [ p.name, p.id ] }, {:include_blank => true} %>
</div>

It feels good to make progress, and I think I should be talking more specifically about what I've learned. I feel like my learning has been all over the board, but maybe that's how everyone learns to code.