Hidden Features of Ruby on Rails [closed]

To avoid duplicate form submissions, Rails has a nice option for submit tags:

submit_tag "Submit", :disable_with => "Saving..."

This adds behavior to the submit button to disable it once clicked, and to display "Saving..." instead of "Submit".

Rails 4+

 DEPRECATION WARNING: :disable_with option is deprecated and 
 will be removed from Rails 4.1. Use 'data: { disable_with: 'Text' }' instead.

Thus the above becomes:

submit_tag 'Submit', data: { disable_with: 'Text' }

integer.ordinalize is one little method that I just stumbled upon not to long ago.

1.ordinalize = "1st"
3.ordinalize = "3rd"

I'm currently in love with div_for and content_tag_for

<% div_for(@comment) do %>
  <!-- code to display your comment -->
<% end %>

The above code renders this:

<div id="comment_123" class="comment">
  <!-- code to display your comment -->
</div>

Want the CSS class to be comment other_class? No problem:

<% div_for(@comment, :class => 'other_class') do %>
  <!-- code to display your comment -->
<% end %>

Want a span and not a div? No problem, content_tag_for to the rescue!

<% content_tag_for(:span, @comment) do %>
<% end %>

# Becomes...

<span id="comment_123" class="comment">
  <!-- code to display your comment -->
</span>

content_tag_for is also great if you want to prefix you id. I use it for loading gifs.

<% content_tag_for(:span, @comment, 'loading') do %>
  <%= image_tag 'loading.gif' -%>
<% end %>

# Becomes...

<span id="loading_comment_123" class="comment">
  <img src="loading.gif" />
</span>

To see a list of gems that are installed, you can run:

gem server

Then point your browser at:

http://localhost:8808

You get a nicely formatted list of your gems with links to rdoc, the web and any dependencies. Much nicer than:

gem list