Eli showed me this code for a menu partial, which I put at the top of the Character view page. It allows you to switch to the view of a different model that belongs_to Character.
<%= link_to_unless_current 'Character', character_path(@character) %>
<%= link_to_unless_current 'Armor', character_armor_path(@character, @character.armor) %>
So this links to Armor's view page. Pretty awesome, except it doesn't work if Armor hasn't been built yet, which will be the case whenever creating a new character. Ops! I thought putting a method for creating a new instance of Armor if one didn't exist would fix this problem, but I couldn't get that to work, either. Also, I'm not convinced that's the best way to handle this problem. For another thing, I want to link to an Item page, not an Armor page, since I see Armor as being an Item.
I updated that code and here's what we've got now:
<%= link_to_unless_current 'Character', character_path(@character) %>
<%= link_to_unless_current 'Items', character_item_path(@character, @character.item) %>
However, this gives me a new error.
No route matches {:controller=>"items", :action=>"show", :id=>nil, :character_id=>#<Character id: 7, name: "Test" ... updated_at: "2011-06-04 15:43:59">
I'm not sure why I'm getting a routing error. Here's my route code:
resources :characters do
resources :statistics
...
resources :items
end
Here's what I'm trying to do with this menu partial. I'll want to have 5 links: Character, Items, Skills and Feats, Spells and Background. Much like the code David showed me for creating Statistics, I know I can use this method for all of them once I have it down.
And here's what I want the links to do. Clicking on Items will allow a user to create various types of items associated with that character: weapons, armor, pimp canes and so on. At this point I'm assuming Item will need to be a separate model, because it will have a belongs_to relationship with Character, right? Even though it's just going to be used as a view that displays forms for creating other models?
And Character should have a has_one relationship with Item, right? It's just going to be a single page, and while a Character can have many weapons, armors and pimp canes, I'll only be working with one Item view.
I'll have a day where everything I try to do works and I'm making great progress and I'll feel like the champion of code, then the next day I'll switch gears slightly and feel like I'm adrift on a hostile sea of errors. Anyone else have this same experience when you were learning Rails?