Module: Merb::Helpers::Form

Defined in:
lib/merb_forgery_protection/form_helpers.rb

Instance Method Summary collapse

Instance Method Details

#form_for(obj, attrs = {}, &block) ⇒ Object

Generates a resource specific form tag which accepts a block, this also provides automatic resource routing.

<% form_for :person, :action => url(:people) do %>
  <%= text_control :first_name, :label => 'First Name' %>
  <%= text_control :last_name,  :label => 'Last Name' %>
  <%= submit_button 'Create' %>
<% end %>

The HTML generated for this would be:

<form action="/people/create" method="post">
  <label for="person[first_name]">First Name</label><input id="person_first_name" name="person[first_name]" size="30" type="text" />
  <label for="person[last_name]">Last Name</label><input id="person_last_name" name="person[last_name]" size="30" type="text" />
  <input name="commit" type="submit" value="Create" />
</form>


40
41
42
43
44
45
46
47
48
49
# File 'lib/merb_forgery_protection/form_helpers.rb', line 40

def form_for(obj, attrs={}, &block)
  set_multipart_attribute!(attrs)
  obj = obj_from_ivar_or_sym(obj)
  fake_form_method = set_form_method(attrs, obj)
  concat(open_tag("form", attrs), block.binding)
  concat(generate_fake_form_method(fake_form_method), block.binding) if fake_form_method
  fields_for(obj, attrs, &block)
  concat(token_field, block.binding) if attrs[:method] == :post
  concat("</form>", block.binding)
end

#form_tag(attrs = {}, &block) ⇒ Object

Generates a form tag, which accepts a block that is not directly based on resource attributes

<% form_tag(:action => url(:controller => "foo", :action => "bar", :id => 1)) do %>
  <%= text_field :name => 'first_name', :label => 'First Name' %>
  <%= submit_button 'Create' %>
<% end %>

The HTML generated for this would be:

<form action="/foo/bar/1" method="post">
  <label for="first_name">First Name</label><input id="first_name" name="first_name" size="30" type="text" />
  <input name="commit" type="submit" value="Create" />
</form>


16
17
18
19
20
21
22
23
24
# File 'lib/merb_forgery_protection/form_helpers.rb', line 16

def form_tag(attrs = {}, &block)
  set_multipart_attribute!(attrs)
  fake_form_method = set_form_method(attrs)
  concat(open_tag("form", attrs), block.binding)
  concat(generate_fake_form_method(fake_form_method), block.binding) if fake_form_method        
  concat(capture(&block), block.binding)
  concat(token_field, block.binding) if attrs[:method] == :post
  concat("</form>", block.binding)
end