Helper methods to ease form creation
Ruby
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib
spec
.gitignore
MIT-LICENSE
README.rdoc
Rakefile
TODO
init.rb

README.rdoc

ExtendedFormHelper

Helper methods to ease form creation

Note that all e_xxx are just wrapers, so all non mentioned options are send directly to underlying method

All methods work as expected in form_for:

<% form_for(:page) do |f| %>
  <%= f.e_text_field(:title, :label => true) %>
  <%= f.e_text_area(:text, :label => true, :before => '<span>description</span>') %>
  <%- f.e_check_box(:agree, :label => true) do |c| -%>
    <%= c %>
    <%= render 'agreement' %>
  <%- end -%>
  <%= f.e_submit %>
<% end %>

Examples

e_text_field

<%= f.e_text_field(:title) %>

<div class="control textField">
  <input id="page_title" name="page[title]" size="30" type="text" value="qwerty" />
</div>

with hint:

<%= f.e_text_field(:title, :hint => 'input some text') %>

<div class="control textField">
  <input class="hint" id="page_title__hint" onFocus="document.getElementById('page_title__hint').style.display='none';document.getElementById('page_title').style.display='';document.getElementById('page_title').focus();" style="display: none;" type="text" value="input some text" />
  <input id="page_title" name="page[title]" onBlur="if(this.value==''){document.getElementById('page_title__hint').style.display='';document.getElementById('page_title').style.display='none';}; " size="30" type="text" value="qwerty" />
</div>

with error:

<%= f.e_text_field(:title) %>

<div class="control textField">
  <div class="fieldWithErrors">
    <input id="page_title" name="page[title]" size="30" type="text" value="qwerty" />
  </div>
  <div class="formError">not good</div>
</div>

with errors:

<%= f.e_text_field(:title) %>

<div class="control textField">
  <div class="fieldWithErrors">
    <input id="page_title" name="page[title]" size="30" type="text" value="qwerty" />
  </div>
  <ul class="formErrors">
    <li class="formError">not good</li>
    <li class="formError">not bad</li>
  </ul>
</div>

with label:

<%= f.e_text_field(:title, :label => 'Input text') %>

<div class="control textField">
  <label for="page_title">Input text</label>
  <input id="page_title" name="page[title]" size="30" type="text" value="qwerty" />
</div>

with label from I18n (define translation for 'page.title'):

<%= f.e_text_field(:title, :label => true) %>

<div class="control textField">
  <label for="page_title">special label</label>
  <input id="page_title" name="page[title]" size="30" type="text" value="qwerty" />
</div>

with class:

<%= f.e_text_field(:title, :class => 'superField') %>

<div class="control textField superField">
  <input id="page_title" name="page[title]" size="30" type="text" value="qwerty" />
</div>

with content before and after:

<%= f.e_text_field(:title, :before => '<span>before</span>', :after => '<span>after</span>') %>

<div class="control textField">
  <span>before</span>
  <input id="page_title" name="page[title]" size="30" type="text" value="qwerty" />
  <span>after</span>
</div>

in block form:

<%- f.e_text_field(:title) do |c| -%>
  <span>before</span>
  <%= c %>
  <span>after</span>
<%- end -%>

<div class="control textField">
  <span>before</span>
  <input id="page_title" name="page[title]" size="30" type="text" value="qwerty" />
  <span>after</span>
</div>

besides to_str method (called implicitly - concats label, control and errors), there are methods label, control and errors

e_text_area, e_file_field, e_check_box

same as e_text_field:

<%= f.e_text_area(:title, :label => 'Input text') %>
<%= f.e_file_field(:title, :label => 'Input text') %>
<%= f.e_check_box(:title, :label => 'Input text') %>

<div class="control textArea">
  <label for="page_title">Input text</label>
  <textarea cols="40" id="page_title" name="page[title]" rows="20">qwerty</textarea>
</div>
<div class="control fileField">
  <label for="page_title">Input text</label>
  <input id="page_title" name="page[title]" size="30" type="file" />
</div>
<div class="control checkBox">
  <input name="page[title]" type="hidden" value="0" />
  <input id="page_title" name="page[title]" type="checkbox" value="1" />
  <label for="page_title">Input text</label>
</div>

e_password_field

same as e_text_field, but value is not automatically inserted:

<%= f.e_password_field(:title, :label => 'Input text') %>

<div class="control passwordField">
  <label for="page_title">Input text</label>
  <input id="page_title" name="page[title]" size="30" type="password" value="" />
</div>

pass :value => true to insert:

<%= f.e_password_field(:title, :value => true) %>

<div class="control passwordField">
  <input id="page_title" name="page[title]" size="30" type="password" value="qwerty" />
</div>

e_submit

<%= f.e_submit('save') %>

<div class="control submit">
  <input name="commit" type="submit" value="save" />
</div>

getting values from I18n (define translation for 'page.create' and 'page.save'):

<%= f.e_submit %>

<div class="control submit">
  <input name="commit" type="submit" value="Create page!" />
</div>

custom control

def trigger(object_name, method, options)
  '<div>i am trigger</div>'
end
<%= f.e_control(:trigger, :switch_column) %>

<div class="control trigger">
  <div>i am trigger</div>
</div>

as block:

def super_trigger(object_name, method, options)
  '<div>i am super trigger</div>'
end
<%- e_control(:super_trigger, :switch_column) do |c| -%>
  <span>before</span>
  <%= c %>
  <span>after</span>
<%- end -%>

<div class="control superTrigger">
  <span>before</span>
  <div>i am super trigger</div>
  <span>after</span>
</div>