Well, after 5 days of no internet access (cable modem fried) I am finally able to get back to Ruby on Rails. And I have quite a bit to talk about, if only I had the time to go over it all right now.
First off, I have to say that although there is documentation in the Rails Framework API I have run into my fair share of unclear explanations. So I want to set a few things straight, in my own words.
Let us start with something simple - the text_area tag. In the rails API it tells us to do the following to adjust size:
<%= text_area_tag "body", nil, :size => "25x10" %>
However, I have found it much easier and less problematic (although the problems could be due to the typist) to just use:
<%= text_area 'object', 'name', :cols => "40", :rows => "5" %>
That will create an easily modified text_area for us to use in our form. Simple enough, but somehow I kept getting things confused, perhaps it was due to the difference between text_area and text_area_tag, although I would think them the same.
Now I want to print out the number of comments for each of my posts (Should be visible on the page). So I decided to use the .count() function that comes with rails. I already had my Model called comment and corresponding table comments, so it should be quite easy to print out a link with the .count of comments for each article. I consulted the API and found this example (and additional information):
:conditions: An SQL fragment like "administrator = 1" or [ "user_name = ?", username ]. See conditions in the intro.
Person.count(:conditions => "age > 26")
Do they test these? I don't know, but I had no luck setting up my Comment.count(:conditions => "thought_id = 1")... much less anything a bit more complicated! After a bit of playing around, testing, and thinking about how it should be I got it to work the way I wanted. However it did not have the :conditions in it~! I eventually discovered the simplicity that the API did not clearly present:
Comment.count(["thought_id = ?", thought.id])
"an option hash as the only parameter"
Sadly that quote came from the API, and that is what confirmed my suspicion that the examples where not working versions. Blah.
Finally I want to cover another 'simple' topic. I want to create custom hyperlink text that includes both dynamic and static text. Using the link_to tag I had a great time trying to figure how I could use my Comment.count() to show the number of comments in the link.
However, the only examples in the API are of static link text that have dynamic href="" values. Nothing different from this tag in my admin_controller:
<%= link_to 'View', :action => 'viewpost', :id => thought %>
Sure, the each link will take you to a different post, but the links all read the same -- 'View'. Very dull, very boring.. .no fun at all. And what do the four(4) examples in the API show?
link_to "Delete this page"
link_to "Help"
link_to "Busy loop"
link_to "Destroy account"
Wonderful! Well it was time to dig in again and figure out how to display the dynamic amount of comments, and the word 'comments' in one link for my site. To make things short and simple this is an example of what it required in my case:
<%= link_to [Comment.count(["thought_id = ?", thought.id]), " comments" ], :action => 'comment', :id => thought %>
As you can see it required a hash, with the Comment.cout() as the first element of the hash, and the static " comments" as the second value.
If there is anything I have truly learned about Ruby on Rails, it is that Hash is king. When in doubt use a hash and you will most likely be rewarded with the results you hoped for.
And once you learn that, things will undoubtedly get smoother riding the rails. In any case, Happy coding =)
