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: