Today I finished up Skyrim Stories. If you'll recall, last time I posted about this project, I was looking for a way to strip out #skyrimstories from the posted tweets as well as a way to make the usernames into links to that poster's Twitter account.
My friend Matt sent me to W3 Schools to learn how to use string replace with Javascript. Took me a minute to figure out how this worked until I realized I needed to call a string replace method on the strings BEFORE they were collected into an array.
I was able to use Twitter Anywhere to make usernames into links. Plus it's got a slick hover card feature with a +Follow button. This API is easy to understand and did exactly what I wanted done plus a few extra things.
For the most part I think I'm done with this project now. I might tweak it a bit here and there, but I think it's pretty solid for what it does. And it's time for me to get back into some Ruby work, anyway.
My name is Tyler. I'm learning Ruby on Rails. This is a blog for me to talk about what I've learned. I'm no longer completely new to Ruby and while I don't feel comfortable calling myself a coder or programmer yet, I am absolutely a Ruby Enthusiast and a Rails Fancier. I've been working with Ruby on Rails since February 1, 2011. You know that song about "lovin' every minute of it?" Bingo.
Showing posts with label API. Show all posts
Showing posts with label API. Show all posts
Sunday, November 27, 2011
More work on Skyrim Stories
Wednesday, June 22, 2011
Rails Validation Syntax
Tonight while working through Agile Web Development with Rails, I was finishing up Chapter 7 and doing some of the extra stuff at the end. One of the exercises was adding validation to the Product model that checks the length of product titles is at least 10 characters.
No big deal! I fired up Rails API and found what I was looking for.
Ahh, perfect! Plus, editing the default message is the second optional exercise. Easy enough. But wait, what's this?
The book had me write validation like so:
This is a slightly different syntax than the validation I found on the Rails API and I don't like it as much. First, it's not as immediately obvious what this validation is doing. Second, it just feels clunkier. So since I'd already written a validation method in a different syntax, I figured this would do the same thing and be in a more readable format:
And what do you know? It works! Is there anything I should be aware of when writing validation like this? Any ideas why Agile Web Development with Rails wrote it the other way? Personal preference of the programmer, perhaps? I'm assuming this is a standard way of writing validation, both because it's the same syntax I found on the Rails API, and because it's more readable. (It's also 5 characters shorter, but who's counting? A piece of Javascript I found online, that's who!)
Ahh, feels good to be making progress. I got hung up for a bit writing tests in the last chapter, until I realized I'd added a stray period and it was holding everything up. Anyone else try running test:unites by mistake? I've probably done it 3 or 5 times in the last 18 hours.
No big deal! I fired up Rails API and found what I was looking for.
validates_length_of :title, :minimum => 10, :message => 'must be at least 10 characters.'
Ahh, perfect! Plus, editing the default message is the second optional exercise. Easy enough. But wait, what's this?
The book had me write validation like so:
validates :price, :numericality => {:greater_than_or_equal_to => 0.01}, :message => 'must be a number.'
This is a slightly different syntax than the validation I found on the Rails API and I don't like it as much. First, it's not as immediately obvious what this validation is doing. Second, it just feels clunkier. So since I'd already written a validation method in a different syntax, I figured this would do the same thing and be in a more readable format:
validates_numericality_of :price, :greater_than_or_equal_to => 0.01, :message => 'must be a number.'
And what do you know? It works! Is there anything I should be aware of when writing validation like this? Any ideas why Agile Web Development with Rails wrote it the other way? Personal preference of the programmer, perhaps? I'm assuming this is a standard way of writing validation, both because it's the same syntax I found on the Rails API, and because it's more readable. (It's also 5 characters shorter, but who's counting? A piece of Javascript I found online, that's who!)
Ahh, feels good to be making progress. I got hung up for a bit writing tests in the last chapter, until I realized I'd added a stray period and it was holding everything up. Anyone else try running test:unites by mistake? I've probably done it 3 or 5 times in the last 18 hours.
Labels:
agile web development,
API,
javascript,
rails,
syntax,
tests,
validation
Subscribe to:
Posts (Atom)