Home
Ruby on Rails
Helpers
Rails' helpers are most commonly used when you're creating HTML forms. Here are some helpers you've probably used:
form_fortext_fieldcollection_selectThey 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.
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.
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.
Add new comment
Create forms, check boxes, radio buttons, select lists and more using Rails' built-in form helpers.
More