The Active Record Helper makes it easier to create forms for records kept in instance variables. The most far-reaching is the form method that creates a complete form for all the basic content types of the record (not associations or aggregations, though). This is a great of making the record quickly available for editing, but likely to prove lacklusters for a complicated real-world form. In that case, it’s better to use the input method and the specialized form methods in classes/ActionView/Helpers/FormHelper.html
Returns a string containing the error message attached to the method on the object, if one exists. This error message is wrapped in a DIV tag, which can be specialized to include both a prepend_text and append_text to properly introduce the error and a css_class to style it accordingly. Examples (post has an error message "can’t be empty" on the title attribute):
<%= error_message_on "post", "title" %> =>
<div class="formError">can't be empty</div>
<%= error_message_on "post", "title", "Title simply ", " (or it won't work)", "inputError" %> =>
<div class="inputError">Title simply can't be empty (or it won't work)</div>
[ show source ]
# File lib/action_view/helpers/active_record_helper.rb, line 68
68: def error_message_on(object, method, prepend_text = "", append_text = "", css_class = "formError")
69: if instance_eval("@#{object}").errors.on(method)
70: "<div class=\"#{css_class}\">#{prepend_text + instance_eval("@#{object}").errors.on(method) + append_text}</div>"
71: end
72: end
Returns an entire form with input tags and everything for a specified Active Record object. Example (post is a new record that has a title using VARCHAR and a body using TEXT):
form("post") =>
<form action='create' method='POST'>
<p>
<b>Title</b><br />
<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />
</p>
<p>
<b>Body</b><br />
<textarea cols="40" id="post_body" name="post[body]" rows="20" wrap="virtual">
Back to the hill and over it again!
</textarea>
</p>
<input type='submit' value='Create' />
</form>
It’s possible to specialize the form builder by using a different action name and by supplying another block renderer. Example (entry is a new record that has a message attribute using VARCHAR):
form("entry", :action => "sign", :input_block =>
Proc.new { |record, column| "#{column.human_name}: #{input(record, column.name)}<br />" }) =>
<form action='sign' method='POST'>
Message:
<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" /><br />
<input type='submit' value='Sign' />
</form>
[ show source ]
# File lib/action_view/helpers/active_record_helper.rb, line 47
47: def form(record_name, options = {})
48: record = instance_eval("@#{record_name}")
49: action = options[:action] || (record.new_record? ? "create" : "update")
50: id_field = record.new_record? ? "" : InstanceTag.new(record_name, "id", binding).to_input_field_tag("hidden")
51:
52: "<form action='#{action}' method='POST'>" +
53: id_field + all_input_tags(record, record_name, options) +
54: "<input type='submit' value='#{action.gsub(/[^A-Za-z]/, "").capitalize}' />" +
55: "</form>"
56: end
Returns a default input tag for the type of object returned by the method. Example (title is a VARCHAR column and holds "Hello World"):
input("post", "title") =>
<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />
[ show source ]
# File lib/action_view/helpers/active_record_helper.rb, line 15
15: def input(record_name, method)
16: InstanceTag.new(record_name, method, binding).to_tag
17: end
[ show source ]
# File lib/action_view/helpers/active_record_helper.rb, line 75
75: def all_input_tags(record, record_name, options)
76: input_block = options[:input_block] || default_input_block
77: record.class.content_columns.collect{ |column| input_block.call(record_name, column) }.join("\n")
78: end
[ show source ]
# File lib/action_view/helpers/active_record_helper.rb, line 80
80: def default_input_block
81: Proc.new { |record, column| "<p><b>#{column.human_name}</b><br />#{input(record, column.name)}</p>" }
82: end