You are here: Home Solidarrow Ruby on Rails Solidarrow Helpers

Quickly debugging form helpers

Sometimes you want to quickly see the output of helper methods, and constantly clicking refresh in your browser then viewing the page source can be tiresome. Instead, use the Rails console to check helpers are doing what you want them to.

 

What are helpers again?

Rails' helpers are most commonly used when you're creating HTML forms. Here are some helpers you've probably used:

  • form_for
  • text_field
  • collection_select

They are methods available to the view that make creating HTML code a lot easier. When you're in the process of learning what they do, it's really useful to be able to instantly see the HTML they create, and a way of doing this is to use the Rails console.

The Rails console

Go to the root directory of your application, then type the following...

    ruby script/console

After a few seconds you should see something like this...

    Loading development environment (Rails 2.0.2)
    >>

You can now start interacting with your Rails application via this command line interface.

Example

To use helper methods, you need to prefix them with helper. Let's test out the text_field helper...

    >> helper.text_field(:book, :title)
    => "<input id=\"book_title\" name=\"book[title]\" size=\"30\" type=\"text\" />"
    >>

Or maybe we want to see how radio_button works...

    >> helper.radio_button(:wallpaper, :colour, "Green")
    => "<input id=\"wallpaper_colour_green\" name=\"wallpaper[colour]\" type=\"radio\" value=\"Green\" />"
    >>

As you can see, this is a really good way to get a feel for Rails' helpers, as you can see the results of your code instantly.

 

* Comments

 

> RELATED ARTICLE

* Form Helpers Cheatsheet

Create forms, check boxes, radio buttons, select lists and more using Rails' built-in form helpers. > More