Each... or else justinweiss.com
each, but I have a problem with it. How do you handle an empty collection?
methodmethod. Use it well, and you can learn about your dependencies, save yourself hours of debugging, and get your code to the places it needs to be.
respond_toblocks. But some of your actions, like
index, don't have them. And those actions, when hit with a bad format, will raise the wrong kind of error. You want to handle bad formats correctly, but littering all your controllers with big
respond_toblocks is crazy. It feels un-Rails-ish. So what do you do?
NoMethodError: undefined method '' for nil:NilClass, anyway? When you learn an API, framework, or library, you can't just learn how to use it when things go well. You also need to figure out what to do when it hands an error back.
current_usermethod returns a
User, except when there is no user and it returns
nil. A search method returns an Array of results, unless there’s only one result, and it returns just that result instead. Seems reasonable, right? Maybe even convenient! But soon, these decisions will bury your code under a mountain of
ifstatements. There is a way to prevent this, though, and all it takes is a little thoughtfulness.
awesome_printin your Rails consoles. Or you might want
rails newto create projects that use rspec instead of minitest. Sure, it's only a little annoying to have to specify these preferences each time you run a command. But they're easy to forget. With a few small tweaks, you can have these commands remember your preferences, so you won't have to.
ifstatements keeps staring you in the face. You feel like you should be able to simplify it, except for that Business Logic that keeps getting in the way. For example, say you have a sales platform where you build
Quotes, which have many
LineItems. Except, you can have a quote with duplicate line items if they’re ads, but if you have multiple websites, you have to sum the prices together and have it show up as a single line item. Oh and also, if you buy a website and already have five ads in your quote, you have to give them a 20% discount on the website.
touch: trueon one of your associations, or your template dependencies aren’t working right, your cached partials won’t get updated. Rails' development environment usually runs with caching disabled, so you'll need a simple way of enabling caching temporarily to investigate and reproduce your caching bugs.
rails newon your next project. Now what? Where do you start? There are tons of decisions you have to make up front, and it’s easy to feel lost, and procrastinate, and get frustrated, and never finish your app.