My name is Tyler. I'm learning Ruby on Rails. This is a blog for me to talk about what I've learned. I'm no longer completely new to Ruby and while I don't feel comfortable calling myself a coder or programmer yet, I am absolutely a Ruby Enthusiast and a Rails Fancier. I've been working with Ruby on Rails since February 1, 2011. You know that song about "lovin' every minute of it?" Bingo.
Wednesday, September 7, 2011
More has_one model work in Rails
I've been coding now for a little over 2 hours and I've finally gotten a problem straightened out. If you'll recall, I was attempting to build a Background for a Character, have it show up in my menu partial, and editable from there. I had a method that would build models with a has_many relationship to Character, but Background will have a has_one relationship and it was proving a bit trickier. I've more or less got it straightened out now (at least it's working on my local machine) so I've thrown it up on Heroku.
Manticore on Heroku
I've also done some more work, simplifying the data that's actually displayed in the view, while still allowing users to manipulate that data with an edit action. My reasoning is a user may always want to know what his total Armor Class is, but how often do you need to know what your Deflection modifier is? Not often, that's how! So basically, if you're rarely going to need it, why display it all the time? I think eventually I'd like to have an arrow users can click on that might expand that information, while displaying the bare minimum by default. I'm not sure what I'd use for this, but it sounds like a problem for Javascript.
Also, don't mind those saving throws! They refuse to work. What a lazy model! This merits a bit of examination. And I actually just realized it's not working when creating a new character on Heroku. Thanks, Heroku!
Still, it feels good to have this problem figured out. I've been wrestling with it for a while, and now I can move on to wrestle with other, more interesting problems! Like this mysterious SAVING THROW problem that's arisen. What could be going on here?
Absolutely LOVE crossing things off my list. Although I appear to have added several more. As usual.
Saturday, June 4, 2011
A little Saturday morning code jam
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?
Monday, May 2, 2011
Mo models, mo problems
Keeping my old code was a good idea, because I'm still referencing it. So far, I'm able to create a new character, then create statistics for each character. However, I'm still having a couple of problems. Each character can still have multiple statistics and I don't have a way to simply update that characters' statistics. Tomorrow I'm going to look through the old code because the first time around I generated a scaffold for statistics and it generated a method for editing.
I've been a bit discouraged because I didn't make much progress this weekend. And in the application I'm building with Agile Web Development, I hit a wall because I edited the code to force users to log in before proceeding without creating a user first. So now I can't view the changes I've made until I either create a user somehow or undo the last dozen or so edits. Needless to say, I've been discouraged from working any further on this project.
Is there a way to simply add a user to a database? Maybe with rails console? I might look into that, but for now I'm focused on working on Manticore again, especially since I'm making some progress.
A few more questions. I'm still unclear on has_one vs. has_many. For example, I have this code for the character model:
class Character < ActiveRecord::Base validates :name, :presence => true
validates :profession, :presence => true
validates :level, :presence => true
has_many :statistics
end
Is has_many the correct association to make here? The statistic model is a table with 6 separate stats. So does it count as one table, or six individual elements? has_many is working, but I thought if I wrote it as has_one, I might avoid the problem of having multiple instances of the statistic model.
And how does sqlite3 handle foreign keys? I'm still unclear on exactly WHAT a foreign key is. Here's my guess:
class StatisticsController < ApplicationController
def create
@character = Character.find(params[:character_id])
@statistic = @character.statistics.create(params[:statistic])
redirect_to character_path(@character)
end
end
In this code from the statistics controller, I'm assuming the foreign key is @character in the @character.statistics.create declaration?
I kind of feel like some very basic stuff is giving me trouble, but I'm still learning and having fun, so onward!