Ruby on Rails Crumbs - Part 1
May 2, 2008
If you have been following along, I blogged previously about my experience trying Ruby on Rails as a .net developer (here and here). I quickly realized how different things are and that I desperately need some help, so I got myself the beta PDF version of Agile Web Development with Rails and Rails Recipes. Both great books but not Rails Recipes doesn’t cover rails latest version - 2.0.
As I read through these books, I realized there are lots of things I need to keep track of, that is lots of crumbs. This series of blog posts are a way for me to keep track of these crumbs as well as share them with others. I don’t plan to explain everything in detail (because I can’t). This is more of a quick reference to remind me of features and things that I read about in the books because there is no FREAKING INTELLISENSE in rails. You will realize how valuable Intellisense is when you don’t have it.
These crumbs are not in any specific order. For the most part, they are in the order I discovered them.
content_tag
Use it to surround a block of text with a tag. The code below creates <b>make me bold</b>
content_tag (:b, “make me bold”)
&block
I can’t find a good explanation of this but when you call a method from your views like this
<% my_foo() do %>
[content]
<% end %>
And the method is defined like this
def my_foo(&block)
end
Then the &block parameter will contain all the text that is wrapped by this method in the view. In the above example &block would contain [content]
blind_down
Use to generate a blind down effect in JavaScript to make hiding an element smoother than just setting display to none. In you rjs template, write:
page[:element_id_to_blind].visual_effect :blind_down
request.xhr?
This returns true if the request is xhr i.e. an AJAX request. I like the trailing question mark. In .net, it would be named IsXhr.
has_many
Use in your model to create a relationship. For example, an article model class might look like this
class Article < ActiveRecord::Base
has_many :categories
end
NOTE: your category model class name is Category but the has_many uses the actual table name which categories. This is so weird for me - no compiler errors, no warnings, nothing. I know, I know, convention over configuration.
belongs_to
Same idea as has_many but in reverse, so the category model class would look like this
class Category < ActiveRecord::Base
belongs_to :articles
end
button_to
A helper method that generates an HTML form containing a button that submits to a URL when clicked. For example:
button_to “Edit”, :action => ‘edit’, :id => 3
Creates a form with an Edit button that submits to the edit action and pass 3 as an id parameter. If we were editing an article, the generated html looks like this:
<form class=”button-to” action=”/articles/edit/3″ method=”post”>
<div><input type=”submit” value=”Edit” /></div>
</form>
More crumbs to come - stay tuned. Also feel free to add your own crumbs to the comments, it might help someone.
Posted in
content rss

May 2nd, 2008 at 11:34 am
As another .net guy just looking into Rails your notes are helpful.
May 2nd, 2008 at 3:46 pm
@Matthew I am glad to hear that all this writing is not going to waste :) Thanks for the comment and you might like my new “crumbs” post. I will keep adding more as I learn my ways.