Form helpers provide a number of methods to simplify the creation of HTML forms. They can work directly with models (bound) or standalone (unbound).
# File lib/merb-helpers/form/helpers.rb, line 19 19: def _new_form_context(name, builder) 20: if name.is_a?(String) || name.is_a?(Symbol) 21: ivar = instance_variable_get("@#{name}") 22: else 23: ivar, name = name, name.class.to_s.snake_case.split('::').last 24: end 25: builder ||= current_form_context.class if current_form_context 26: (builder || self._default_builder).new(ivar, name, self) 27: end
# File lib/merb-helpers/form/helpers.rb, line 5 5: def _singleton_form_context 6: self._default_builder = Merb::Helpers::Form::Builder::ResourcefulFormWithErrors unless self._default_builder 7: @_singleton_form_context ||= 8: self._default_builder.new(nil, nil, self) 9: end
Provides a generic HTML checkbox input tag. There are two ways this tag can be generated, based on the option :boolean. If not set to true, a “magic” input is generated. Otherwise, an input is created that can be easily used for passing an array of values to the application.
method | Resource attribute |
attrs | HTML attributes and options |
String | HTML |
<%= check_box :name => "is_activated", :value => "1" %> <%= check_box :name => "choices[]", :boolean => false, :value => "dog" %> <%= check_box :name => "choices[]", :boolean => false, :value => "cat" %> <%= check_box :name => "choices[]", :boolean => false, :value => "weasle" %>
Used with a model:
<%= check_box :is_activated, :label => "Activated?" %>
# File lib/merb-helpers/form/helpers.rb, line 175 175: def check_box; end
# File lib/merb-helpers/form/helpers.rb, line 15 15: def current_form_context 16: form_contexts.last || _singleton_form_context 17: end
Provides a HTML formatted display of resource errors in an unordered list with a h2 form submission error
obj | Model or Resource |
error_class | CSS class to use for error container |
build_li | Custom li tag to wrap each error in |
header | Custom header text for the error container |
before | Display the errors before or inside of the form |
String | HTML |
<%= error_messages_for @person %> <%= error_messages_for @person {|errors| "You can has probs nao: #{errors.size} of em!"} <%= error_messages_for @person, lambda{|error| "<li class='aieeee'>#{error.join(' ')}"} %> <%= error_messages_for @person, nil, 'bad_mojo' %>
# File lib/merb-helpers/form/helpers.rb, line 435 435: def error_messages_for(obj = nil, opts = {}) 436: current_form_context.error_messages_for(obj, opts[:error_class] || "error", 437: opts[:build_li] || "<li>%s</li>", 438: opts[:header] || "<h2>Form submission failed because of %s problem%s</h2>", 439: opts.key?(:before) ? opts[:before] : true) 440: end
Creates a scope around a specific resource object like form_for, but doesnt create the form tags themselves. This makes fields_for suitable for specifying additional resource objects in the same form.
<%= form_for @person do %> <%= text_field :first_name, :label => "First Name" %> <%= text_field :last_name, :label => "Last Name" %> <%= fields_for @permission do %> <%= check_box :is_admin, :label => "Administrator" %> <% end =%> <%= submit "Create" %> <% end =%>
# File lib/merb-helpers/form/helpers.rb, line 111 111: def fields_for(name, attrs = {}, &blk) 112: attrs ||= {} 113: with_form_context(name, attrs.delete(:builder)) do 114: capture(&blk) 115: end 116: end
Provides the ability to create quick fieldsets as blocks for your forms.
attrs | HTML attributes and options |
legend | Adds a legend tag within the fieldset |
String | HTML |
Block helpers use the <%= =%> syntax
<%= fieldset :legend => "Customer Options" do %> ...your form elements <% end =%>
Generates the HTML:
<fieldset> <legend>Customer Options</legend> ...your form elements </fieldset>
# File lib/merb-helpers/form/helpers.rb, line 143 143: def fieldset(attrs = {}, &blk) 144: _singleton_form_context.fieldset(attrs, &blk) 145: end
# File lib/merb-helpers/form/helpers.rb, line 147 147: def fieldset_for(name, attrs = {}, &blk) 148: with_form_context(name, attrs.delete(:builder)) do 149: current_form_context.fieldset(attrs, &blk) 150: end 151: end
Provides a HTML file input
name | Model or Resource |
attrs | HTML attributes |
String | HTML |
<%= file_field :name => "file", :label => "File" %>
Used with a model:
<%= file_field :file, :label => "Choose a file" %>
# File lib/merb-helpers/form/helpers.rb, line 192 192: def file_field; end
Generates a form tag, which accepts a block that is not directly based on resource attributes
attrs | HTML attributes |
String | HTML |
* Block helpers use the <%= =%> syntax * a multipart enctype is automatically set if the form contains a file upload field
<%= form :action => url(:controller => "foo", :action => "bar", :id => 1) do %> <%= text_field :name => "first_name", :label => "First Name" %> <%= submit "Create" %> <% end =%> Generates the HTML: <form action="/foo/bar/1" method="post"> <label for="first_name">First Name</label> <input type="text" id="first_name" name="first_name" /> <input type="submit" value="Create" /> </form>
# File lib/merb-helpers/form/helpers.rb, line 61 61: def form(*args, &blk) 62: _singleton_form_context.form(*args, &blk) 63: end
# File lib/merb-helpers/form/helpers.rb, line 11 11: def form_contexts 12: @_form_contexts ||= [] 13: end
Generates a resource specific form tag which accepts a block, this also provides automatic resource routing.
name | Model or Resource |
attrs | HTML attributes |
String | HTML |
* Block helpers use the <%= =%> syntax
<%= form_for @person do %> <%= text_field :first_name, :label => "First Name" %> <%= text_field :last_name, :label => "Last Name" %> <%= submit "Create" %> <% end =%>
The HTML generated for this would be:
<form action="/people" method="post"> <label for="person_first_name">First Name</label> <input type="text" id="person_first_name" name="person[first_name]" /> <label for="person_last_name">Last Name</label> <input type="text" id="person_last_name" name="person[last_name]" /> <input type="submit" value="Create" /> </form>
# File lib/merb-helpers/form/helpers.rb, line 93 93: def form_for(name, attrs = {}, &blk) 94: with_form_context(name, attrs.delete(:builder)) do 95: current_form_context.form(attrs, &blk) 96: end 97: end
Provides a generic HTML label.
attrs | HTML attributes |
String | HTML |
<%= label "Full Name", :for => "name" %> => <label for="name">Full Name</label>
# File lib/merb-helpers/form/helpers.rb, line 223 223: def label(*args) 224: current_form_context.label(*args) 225: end
Provides a HTML password input.
name | Model or Resource |
attrs | HTML attributes |
String | HTML |
<%= password_field :name => :password, :label => "Password" %> # => <label for="password">Password</label><input type="password" id="password" name="password" />
Used with a model:
<%= password_field :password, :label => 'New Password' %>
# File lib/merb-helpers/form/helpers.rb, line 243 243: def password_field; end
Provides a radio group based on a resource attribute. This is generally used within a resource block such as form_for.
method | Resource attribute |
arr | Choices |
String | HTML |
<%# the labels are the options %> <%= radio_group :my_choice, [5,6,7] %> <%# custom labels %> <%= radio_group :my_choice, [{:value => 5, :label => "five"}] %>
# File lib/merb-helpers/form/helpers.rb, line 282 282: def radio_group; end
Provides a HTML select
method | Resource attribute |
attrs | HTML attributes and options |
prompt | Adds an additional option tag with the provided string with no value. |
selected | The value of a selected object, which may be either a string or an array. |
include_blank | Adds an additional blank option tag with no value. |
collection | The collection for the select options |
text_method | Method to determine text of an option (as a symbol). Ex: :text_method => :name will call .name on your record object for what text to display. |
value_method | Method to determine value of an option (as a symbol). |
String | HTML |
<%= select :name, :collection => %w(one two three) %>
# File lib/merb-helpers/form/helpers.rb, line 303 303: def select; end
Generates a HTML submit button.
value | Sets the value=”“ attribute |
attrs | HTML attributes |
String | HTML |
<%= submit "Process" %>
# File lib/merb-helpers/form/helpers.rb, line 414 414: def submit(contents, attrs = {}) 415: current_form_context.submit(contents, attrs) 416: end
Provides a HTML textarea tag
contents | Contents of the text area |
attrs | HTML attributes |
String | HTML |
<%= text_area "my comments", :name => "comments" %>
Used with a model:
<%= text_area :comments %>
# File lib/merb-helpers/form/helpers.rb, line 320 320: def text_area; end
Provides a HTML text input tag
name | Model or Resource |
attrs | HTML attributes |
String | HTML |
<%= text_field :name => :fav_color, :label => "Your Favorite Color" %> # => <label for="fav_color">Your Favorite Color</label><input type="text" id="fav_color" name="fav_color" />
Used with a model:
<%= form_for @person do %> <%= text_field :first_name, :label => "First Name" %> <% end =%>
# File lib/merb-helpers/form/helpers.rb, line 340 340: def text_field; end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.