Skip to content


Using the form helper in the merb_helper plugin

Merb has a hidden rough gem buried in merb plugins tree. In there, You’ll find the merb_helpers plugin. All merb plugins are gems, and you can build it and install it using “rake install” Once the plugin is installed, you have to enable it in the “config/dependencies.rb” file in your Merb project.

1
2

dependencies "merb_helpers"
<p>After it is enabled, you can use it in your views.</p>


<p>The merb_helper exposes some familiar constructs in some not so familiar ways:</p>


<p>There are two ways to create a form.  You can use form_tag or form_for.  If you have a Rails background, these two will be familiar.</p>


<h3>Creating a form</h3>

You can create a normal form using form_tag

<% form_tag({url(:controller => "foo", :action => "bar", :id => 1)} do %>
form stuff
<% end %>

You can use the following helpers when creating a form with form_tag.

text_field

<%= text_field :name => "text_field", :value => "foo" %>

checkbox_field

<%= checkbox_field :name => "checkbox", :value => "1", :label => "checkbox" %>

hidden_field

<%= hidden_field :name => "hidden_field", :value => "foo" %>

radio_field

<%= radio_field :name => "radio", :value => "1", :label => "radio" %>

text_field Note that this a bit different from the other types.

<%= text_area_field "comments", :name => "comments", :label => "comments" %>

submit_button

<%= submit_button "Submit Me" %>

Create a form for a resource

You can also create a form for a resource using form_for.

<% form_for :person, :action => url(:people) do %>
<% end %>

The form helper also includes some input helpers that work with form_for.

text_control

<%= text_control :name, :label => "Name:" %>

hidden_control

<%= hidden_control :hidden_name %>

checkbox_control

<%= checkbox_control :my_option, :label => "My Option:" %>

radio_group_control

<%= radio_group_control :my_choice, [5,6,7] %>
or
<%= radio_group_control :my_choice, [{:value => 5, :label => "five"}, {:value => 6, :label => "six"}] %>

text_area_control

<%= text_area_control :comments %>

Posted in Uncategorized.