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.
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”)
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
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]
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.
Use in your model to create a relationship. For example, an article model class might look like this
class Article < ActiveRecord::Base
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.
Same idea as has_many but in reverse, so the category model class would look like this
class Category < ActiveRecord::Base
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>
More crumbs to come – stay tuned. Also feel free to add your own crumbs to the comments, it might help someone.