Saturday, July 30, 2011

Models, forms and view pages in Rails

I'm back to working on Manticore after a couple months away doing tutorials. I've made some progress this afternoon, but now I've run into a snag.

Here's what's going on: I have two models, Character and Item. I've created a form_for new Items that belong to a Character. The form works fine when displayed in the Character view, but what I want to do is move the form and Item listing out of the Character view and into the Index page of Item, so I can link to it from the Character view. I've got a _menu partial that I'll be using to display other views relating to Character, such as Skills, Spells, Items and so on. I'm not sure what the issue is, but simply transplanting the code as is gives me this error:

undefined method 'items' for nil:NilClass

Here's the exact code I'm using:

<h2>Add an item:</h2>

<%= form_for ([@character, @character.items.build]) do |f| %>
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :type %><br />
    <%= f.text_field :type %>
  </div>
  <div class="field">
    <%= f.label :location %><br />
    <%= f.text_field :location %>
  </div>
  <div class="field">
    <%= f.label :description %><br />
    <%= f.text_area :description %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

<table>
<% @character.items.each do |item| %>
  <tr>
    <td><%= item.name %></td>
    <td><%= item.type %></td>
    <td><%= item.location %></td>
    <td><%= item.description %></td>
  </tr>
  <table>
<% end %>

Like I said, this works just fine in the character/show page, but does not work when placed in item/index and linked to from my _menu partial. Any ideas?

Here's a link to my git repository:

Sunday, July 24, 2011

More Heroku Rails Deployment Issues

I've (partially) fixed my issues with deploying to Heroku. The solution was to specify rake version 0.8.7, as noted all over Stack Overflow for people who had the same error I was getting. I could have fixed this issue earlier, if I'd realized you deploy to Heroku from Git, not from your local machine. So I was updating my code, then trying to migrate the database on Heroku and I kept getting the same error because the code hadn't been changed on Git. Still, progress!

But I'm still having a problem. Take a look at the deployed version of my Depot application:


And here's a screenshot of how it looks on my machine:


Heroku is not displaying the product listing and I can't figure out why. The database information is stored in a seed file, so I thought a heroku rake db:seed command would populate the data and fix my issue, but no such luck. I've also noticed that the new action for my Product model throws an error on Heroku, but it works on my local version. Any ideas what's going on here?

Here's a link to my git repository for this application:

Saturday, July 23, 2011

Uninitialized Constant Rake::DSL and Heroku

I took @jqr's advice and signed up for Heroku this morning. Agile Web Development had me working on running a virtual name server with Apache and it was way too complicated for my tastes. Apparently coders who don't want to deal with server-side headaches are pretty common, hence Heroku!

But I've hit a snag. I get an uninitialized constant Rake::DSL error whenever I try to run a migration on Heroku. Here's the full error:

gozer /users/tylermoore/code/depot master$ heroku rake db:migrate
(in /app)
rake aborted!
uninitialized constant Rake::DSL
/usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2482:in `const_missing'
/app/Rakefile:6:in `'
/usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2373:in `load'
/usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2373:in `raw_load_rakefile'
/usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2007:in `block in load_rakefile'
/usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2058:in `standard_exception_handling'
/usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2006:in `load_rakefile'
/usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:1991:in `run'
/usr/ruby1.9.2/bin/rake:31:in `

'


Apparently, this is a well-documented error, and I've dug through a ton of Stack Overflow and Google discussion threads, trying various solutions but no luck so far.

I've manually set my rake gem to version 0.8.7, updated my GemFile and pushed changes to Git, which works for a lot of people, but not for me. Still, I'm not discouraged. I'll figure this out eventually. Just thought I'd throw this up here in case someone has had this exact error and found another solution.

Thursday, July 21, 2011

Setting up a virtual Apache server

Holy shit, 10 days without a blog post? Fear not, I have been coding! Just not writing about coding.

So here's where I'm at now. I'm near the end of the practical section of Agile Web Development. The chapter I'm on now is about deployment and I'm having trouble setting up a virtual Apache server and actually getting Depot to deploy. Everything appears to be installed correctly, but I can't get everything tied together correctly.

How do I find my host name? I'll need to define it as depot.hostname.com in my Apache configuration file. I thought it would just be depot.localhost.com, or perhaps depot.127.0.0.1.com, but neither of those worked.

I also had trouble editing my hosts file. I'm able to open it through the terminal, but it won't allow me to make changes.

Still, this headache is a minor set back. I know it's something I need to learn, but for now I think learning more actual coding will be more useful to me. Any good recommendations for new books? I really liked Agile Web Development, so I might look into something else by the Pragmatic Programmers. Not sure if Advanced Rails Recipes would be beyond me or not.

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.