Simply Helpful Rails Plugin

Before I begin let me just state that the Simply Helpful plugin requires Rails 1.2.2. If you already are using Rails 1.2.2 you can continue by downloading the plugin by executing the following command from the root directory of your Rails application using the terminal or command prompt.

script/plugin install simply_helpful

This plugin adds a few additional methods into the action view, action controller, and form helper that you will find more than helpful, if not essential.

In a very Rails like opinionated software fashion, Simply Helpful introduces the view helper method dom_id which will produce the right dom id for a given model instance. Since code is worth a thousands words, and since I can’t type a thousand words in one sitting let me get started with some sample code which can be placed in a rhtml view source file.

<%= dom_id @post %>

If the @post variable was created through a model finder method and it represents a post identified with the primary key of 11, then the above line will print the following string, post_11. Notice that the recommended dom id is the model class name plus the record id. If you are using some cool Scriptaculous effects on the post title and the post body, you will need to uniquely identify each element. You can prefix an additional identifier to the dom id so as to uniquely identify different dom elements for the same post, say the title and the body. In this way, each element can have different effects. Here is an example that will produce the a id which can be used for the given title for the post.

<%= dom_id @post, ‘title’ %>

The above snippet will produce a string that that reads title_post_11. Simply Helpful also provides a dom_class method that will return what it thinks should be the CSS class name for an element for a given model instance. As you can imagine, the dom_class recommends that the parameter’s class name be used as the dom name. Putting together a more complete example, you might have code like the following.

<H1 id=”<%= dom_id @post, ‘title’ %>” class=”<%= dom_class @post %>”>
<%= @post.title %>
</H1>

The dom_id and dom_class methods are pretty helpful but as a co-worker stated, “I would not download a plugin just to concatenate the model class name and id together.” Luckily for my co-worker, Simply Helpful provides a lot more than that, such as form blocks. Here is an example of how I have been doing forms in Rails.

<%= start_form_tag :action => :update, :id => @post %>
<%= text_field ‘post’, ‘title’, :value => @post.title %>
<%= end_form_tag %>

The new form_for method will create a put or post method for a form depending if the model instance is persisted in the database or if it represents a new model. In addition to automatically creating the correct action URL for the form, you have access to a form block variable which can be used to create form fields. Here is an example of a form block.

<% form_for @post do |f| %>
<%= f.text_field :title %>
<% end %>

Compare Simply Helpful’s way of creating forms to the old school Rails ways. It might not be obvious at first, but f.text_field statement will create a text field for the title attribute of the post. If the @post model instance represents and existing record then the current title will be the default value in the text field.

As the name states, Simply Helpful provides a helpful set of methods. Simply Helpful is also required by other Rails plugins such as ResourceFeeder.

Technorati Tags: , , , , , , , , , ,

Related posts:

  1. Riff Rails Plugin
  2. Acts As Voteable Rails Plugin
  3. Updating A Rails Plugin
  4. Rails Plugin Tutorial
  5. Rails Acts As Authenticated Plugin
  6. Acts As Commentable Plugin


4 Responses to “Simply Helpful Rails Plugin”

Leave a Reply