Monday, July 11, 2011

Editing Functional Tests in Rails

There was a time I was afraid of breaking my code. And then there was a time I was less afraid of breaking my code, but I became obsessed with fixing every little error and bug that I ended up wasting time on something relatively trivial when I could be working on more productive matters.

Case in point:

I've been working through the Depot application with Agile Web Development with Rails. This latest chapter had me working through creating a mailer and writing tests for it. Creating the mailer went well, and I can use my own gmail account to send mail through my Depot application! SUCCESS!

However, I got hung up on the functional tests. And I know what the problem is, but not how to fix it.

Here are the errors I get:

1) Error:
test_should_destroy_line_item(LineItemsControllerTest):
ActiveRecord::RecordNotFound: Couldn't find LineItem with ID=980190962 [WHERE ("line_items".cart_id = 980190963)]
app/controllers/line_items_controller.rb:79:in `destroy'
  test/functional/line_items_controller_test.rb:45:in `test_should_destroy_line_item'
  test/functional/line_items_controller_test.rb:44:in `test_should_destroy_line_item'

2) Error:
test_order_shipped(NotifierTest):
ActionView::Template::Error: undefined method `protect_against_forgery?' for #<#:0x1030519d8>
    app/views/line_items/_line_item.html.erb:9:in `_app_views_line_items__line_item_html_erb___1505233713_2172764620_6072364'
    app/views/notifier/order_shipped.html.erb:8:in `_app_views_notifier_order_shipped_html_erb___2127495373_2172826120_0'
    app/mailers/notifier.rb:13:in `order_shipped'
    test/functional/notifier_test.rb:13:in `test_order_shipped'


I have an idea why I'm getting the first error. I've been following the optional exercises at the end of each chapter and I changed the :destroy method in line_items_controller without updating the functional test. Here's the OFFENSIVE code:

def destroy
  @cart = current_cart
  @line_item = @cart.remove_product(@cart.line_items.find(params[:id]))

    respond_to do |format|
      format.html { redirect_to store_url }
      format.js
      format.xml { head :ok }
    end
  end
end


Any idea how to fix this? I'm not super concerned about it, but I'd like to know if I'm on the right track and I think this would be an easy fix. The second error I'm less sure about. I don't get why I'm getting an error for protect_against_forgery? since I'm not calling that method anywhere. Is it a default Rails thing that I'm overlooking?

Anyway, my current strategy has been simply to comment out the tests until later. Which may never come, depending on a variety of factors! I have a sneaking suspicion writing tests will never be one of my strong suits.

Getting back to my original point, it made more sense simply to comment these tests out rather than spend a bunch of time fixing them, especially since my application DOES work. Still, it's a good idea to get a handle on writing tests. I'm doing this for Future Tyler.

Tuesday, July 5, 2011

Ajax and CSS with Rails

So the section I finished up in Agile Web Development with Rails walked me through adding Ajax functionality to the Depot shopping cart application. First I move the shopping cart into the upper left of the sidebar, then added a notification whenever an item was added to the cart without reloading the entire page. Sorcery!

However, the :blind_down visual effect I was using had some issues. Take a look:


Yuck! What are those disgusting bars doing there? Messing up my beautiful flashing green Ajax notification, that's what! There has to be a way to get rid of those. And there is!

Here's the solution. I know the Ajax notification is going off in the shopping cart table. And I know how it's styled in my depot.css file. These are pretty good clues for figuring out how to get rid of those bars. A quick Google search later and I find a little command called border-collapse.

So I just throw that into the css for the cart table:

#cart table {
  border-top: 1px dotted #595;
    ...
    border-collapse: collapse;
}


And here's the result:


Easy enough!

I find myself drawn into doing some styling whenever Agile Web Development is working on it. Styling should really be left until functionality is hammered out, but I like this approach. I like messing around with padding, margins and color schemes. On the other hand, this ties into something I had wrong about coding for a long, long time. I always kind of assumed programmers had a lot more memorized than they do. Now it's apparent that coders memorize methods they use a lot, but still have to look up quite a bit of information. The important thing is understanding how an application works, having a plan to accomplish what you want done, and knowing how to find the specific information when you need it. So in that case, yes, this DOES pertain specifically to Rails and not just CSS!

I also ran across this page while I was searching for Jquery examples. Seems like a good resource.

Monday, July 4, 2011

Rails 3.1 Tutorials

Since I got back into working with Agile Web Development with Rails, I've been thinking about what my next step should be. I suffer under the burden of too many options of things I want to learn and/or work on. I want to work on my knowledge of Ruby, and I think that's a good place to start.

But I just found some Rails 3.1 tutorials on Git the other day, and that sounds like a good choice, too.

Another Rails friend of mine (@SpencerCooley) was talking about how he feels more productive when he's less afraid of breaking his code. I've felt this way for a while now. Remember when I used to just restart an application as soon as I broke the code, because it was less of a hassle to repeat what I knew how to do than to figure out how to fix the things I didn't? How are you supposed to grow and develop working that way? You won't, dummy!

So now that I'm more experienced, I feel way more confident in my ability to not only figure out what is giving me an error, but I feel better equipped to go about trying to solve it. I've got a better grasp on how to talk about what I'm doing. You may have noticed I haven't been updating this blog as often lately. My higher level of confidence in my ability to solve my own problem is partly to blame for this. Still, I need to get back in the habit of updating my blog whenever I code, and I need to get back in the habit of coding every day. No more lazy Fridays, you hear me?

Also, I think I'm just going to use Twitter handles when referring to other Rails enthusiasts from now on.

Monday, June 27, 2011

Session Counter in Rails

Since I've been working through Agile Web Development with Rails, I've felt less like blogging about what I'm doing. It's not that I don't find this book valuable, but I'm less excited about talking about my progress than when I'm working on a personal project. Still, I haven't been slacking in my coding.

I've had a lot of luck translating things I'm learning into methods that work with my other projects (mostly just Chorenivore) and this time through Agile Web Development, I'm doing all the extra stuff. Tonight I learned a method for displaying how many times a user has viewed the index page, as well as a way to reset the session counter and how to change how often it's displayed.

I'm guessing this is the same sort of thing that's used to track how often things are viewed online, right? With some editing, this method could be used on a photo blog to show how many views a particular photo has had, etc. You see how things are coming together?

I've also been using github extensively, mostly just to record what I've done and as a means of source control. Much like firing up the terminal and booting up webrick, I no longer have to think about how to commit changes and push to git. Memorization isn't learning, but it can definitely make learning easier.

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.

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.

Sunday, June 19, 2011

Creating a new application with an older version of Rails

This weekend, I've been working through Agile Web Development with Rails, but I ran into a problem. The book is using Rails version 3.0.5, and I've been updated to Rails 3.1.0.rc2. Some things have been changed from Rails 3.0 to Rails 3.1. Most of these appear to be small, easy to work with changes such as moving images and stylesheets from /public to /assets, but Jquery is the new default javascript library for Rails and this is where I ran into trouble.

First off, I noticed the CSS wasn't working exactly as it did the first time I went through Agile Web Development with Rails. Mostly some minor issues that I was able to work with, and this wasn't that big a deal, since styling is best left until later. But the book deals with some styling up front, so this issue threw a red flag.

Next up, I noticed my :destroy method wasn't working. I ran into this issue a couple weeks ago with Chorenivore, and it was a matter of configuring Jquery. So I tried the fix that worked for me before, but I'm not sure why it wasn't working with a Rails 3.1 app. Is there more configuration needed? I generated the code with a scaffold command, so I'm not sure why it wasn't working by default.

At any rate, whether this is a bug (doubtful, even though this is a beta version of Rails 3.1) or I just didn't configure everything correctly, I decided the best solution for now is to create a new application using the same version used in Agile Web Development with Rails.

So the command for creating a new application with a previous version of Rails is easy enough:

rails _3.0.5_ new depot

In this case, I want to use Rails 3.0.5 and my project is named depot, so that's all there is to it! I think the gemfile may need to be edited as well, but so far I haven't run into any issues.

Rails is a framework that is updated and changed regularly, so I know in the long term I'll want to stay current with new versions. But for now, it's more important for me to keep learning and reinforcing what I already know, so using an older version of Rails is the way to go.

Saturday, June 18, 2011

Applying Tutorial Methods to My Own Rails Projects

So even though I picked Agile Web Development with Rails back up the other day (after about 2 months away from it) I decided to start the entire book over again. I don't typically like doing this, but I felt my reasoning was sound. For one thing, going back through building the shopping cart application would make more sense to me now, since I'm thinking about how Rails applications work and not just making sure I'm following the tutorial correctly so I don't get errors. For another, I was pretty sure I could find some techniques I could apply to my personal projects. Lastly, the first time around I skipped all the optional coding at the end of every section and this time I'm going to do it. I'm more interested in learning new ideas than in making this one specific application work correctly.

On this last part I was correct. I just went through the beginning part, which walks me through generating a scaffold for the project, then some styling to spruce the code up a bit and I already found a couple of ideas I want to apply to Chorenivore.

This is something I need to be doing every time I'm doing a tutorial or looking at code. What can I do with this? How does this apply to my projects?

Something funny I noticed this morning: back when I was learning CSS, I had a hard time understanding what a class was. And now with Rails, it's something I don't even think about. I don't know if I've just seen it often enough that I get it now, or if Rails has so many other things that are more complex that it just doesn't register. Probably a mixture of both, I'm guessing!

I felt a bit lazier this week, because I feel like Chorenivore is about as done as it's going to be for now and I didn't feel like digging back into Agile Web Development with Rails. What do you guys do to stay motivated when you're between projects like this? Does that even happen to more experienced Rails developers?

Oh and one last thing. Eli emailed me asking what happened to my old blog. I didn't realize switching from rubymeltdown.blogspot.com to codeitlikeyoustoleit.blogspot.com would DESTROY all my followers, so I put another blog up on rubymeltdown.blogspot.com, redirecting people to this blog. That should take care of that problem!