0
@@ -8,47 +8,55 @@ module ActionView
0
- # The Active Record Helper makes it easier to create forms for records kept in instance variables. The most far-reaching is the
form0
+ # The Active Record Helper makes it easier to create forms for records kept in instance variables. The most far-reaching is the
+form+0
# method that creates a complete form for all the basic content types of the record (not associations or aggregations, though). This
0
# is a great way of making the record quickly available for editing, but likely to prove lackluster for a complicated real-world form.
0
- # In that case, it's better to use the
input method and the specialized form methods in link:classes/ActionView/Helpers/FormHelper.html
0
+ # In that case, it's better to use the
+input+ method and the specialized +form+ methods in link:classes/ActionView/Helpers/FormHelper.html
0
module ActiveRecordHelper
0
- # Returns a default input tag for the type of object returned by the method. For example, let's say you have a model
0
- # that has an attribute +title+ of type VARCHAR column, and this instance holds "Hello World":
0
- # input("post", "title") =>
0
- # <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />
0
+ # Returns a default input tag for the type of object returned by the method. For example, if <tt>@post</tt>
0
+ # has an attribute +title+ mapped to a +VARCHAR+ column that holds "Hello World":
0
+ # input("post", "title")
0
+ # # => <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />
0
def input(record_name, method, options = {})
0
InstanceTag.new(record_name, method, self).to_tag(options)
0
- # Returns an entire form with all needed input tags for a specified Active Record object. For example, let's say you
0
- # have a table model <tt>Post</tt> with attributes named <tt>title</tt> of type <tt>VARCHAR</tt> and <tt>body</tt> of type <tt>TEXT</tt>:
0
+ # Returns an entire form with all needed input tags for a specified Active Record object. For example, if <tt>@post</tt>
0
+ # has attributes named +title+ of type +VARCHAR+ and +body+ of type +TEXT+ then
0
- # That line would yield a form like the following:
0
- # <form action='/post/create' method='post'>
0
- # <label for="post_title">Title</label><br />
0
- # <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />
0
- # <label for="post_body">Body</label><br />
0
- # <textarea cols="40" id="post_body" name="post[body]" rows="20">
0
- # <input type='submit' value='Create' />
0
+ # would yield a form like the following (modulus formatting):
0
+ # <form action='/posts/create' method='post'>
0
+ # <label for="post_title">Title</label><br />
0
+ # <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />
0
+ # <label for="post_body">Body</label><br />
0
+ # <textarea cols="40" id="post_body" name="post[body]" rows="20"></textarea>
0
+ # <input name="commit" type="submit" value="Create" />
0
# It's possible to specialize the form builder by using a different action name and by supplying another
0
- # block renderer. For example, let's say you have a model <tt>Entry</tt> with an attribute <tt>message</tt> of type <tt>VARCHAR</tt>:
0
+ # block renderer. For example, if <tt>@entry</tt> has an attribute +message+ of type +VARCHAR+ then
0
+ # :input_block => Proc.new { |record, column|
0
+ # "#{column.human_name}: #{input(record, column.name)}<br />"
0
- # form("entry", :action => "sign", :input_block =>
0
- # Proc.new { |record, column| "#{column.human_name}: #{input(record, column.name)}<br />" }) =>
0
+ # would yield a form like the following (modulus formatting):
0
- # <form action='/post/sign' method='post'>
0
- # <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" /><br />
0
- # <input type='submit' value='Sign' />
0
+ # <form action="/entries/sign" method="post">
0
+ # <input id="entry_message" name="entry[message]" size="30" type="text" /><br />
0
+ # <input name="commit" type="submit" value="Sign" />
0
# It's also possible to add additional content to the form by giving it a block, such as:
0
@@ -59,11 +67,11 @@ module ActionView
0
# The following options are available:
0
- # * <tt>action</tt> - the action used when submitting the form (default: create if a new record, otherwise update)
0
- # * <tt>input_block</tt> - specialize the output using a different block, see above
0
- # * <tt>method</tt> - the method used when submitting the form (default: post)
0
- # * <tt>multipart</tt> - whether to change the enctype of the form to multipart/form-date, used when uploading a file (default: false)
0
- # * <tt>submit_value</tt> - the text of the submit button (default: Create if a new record, otherwise Update)
0
+ # * <tt>:action</tt> - The action used when submitting the form (default: +create+ if a new record, otherwise +update+).
0
+ # * <tt>:input_block</tt> - Specialize the output using a different block, see above.
0
+ # * <tt>:method</tt> - The method used when submitting the form (default: +post+).
0
+ # * <tt>:multipart</tt> - Whether to change the enctype of the form to "multipart/form-data", used when uploading a file (default: +false+).
0
+ # * <tt>:submit_value</tt> - The text of the submit button (default: "Create" if a new record, otherwise "Update").
0
def form(record_name, options = {})
0
record = instance_variable_get("@#{record_name}")
0
@@ -84,17 +92,16 @@ module ActionView
0
# Returns a string containing the error message attached to the +method+ on the +object+ if one exists.
0
# This error message is wrapped in a <tt>DIV</tt> tag, which can be extended to include a +prepend_text+ and/or +append_text+
0
# (to properly explain the error), and a +css_class+ to style it accordingly. +object+ should either be the name of an instance variable or
0
- # the actual object. As an example, let's say you have a model
0
- # +post+ that has an error message on the +title+ attribute:
0
+ # the actual object. As an example, let's say you have a model <tt>@post</tt> that has an error message on the +title+ attribute:
0
- # <%= error_message_on "post", "title" %> =>
0
- # <div class="formError">can't be empty</div>
0
+ # <%= error_message_on "post", "title" %>
0
+ # # => <div class="formError">can't be empty</div>
0
- # <%= error_message_on @post, "title" %> =>
0
- # <div class="formError">can't be empty</div>
0
+ # <%= error_message_on @post, "title" %>
0
+ # # => <div class="formError">can't be empty</div>
0
- # <%= error_message_on "post", "title", "Title simply ", " (or it won't work).", "inputError" %> =>
0
- # <div class="inputError">Title simply can't be empty (or it won't work).</div>
0
+ # <%= error_message_on "post", "title", "Title simply ", " (or it won't work).", "inputError" %>
0
+ # # => <div class="inputError">Title simply can't be empty (or it won't work).</div>
0
def error_message_on(object, method, prepend_text = "", append_text = "", css_class = "formError")
0
if (obj = (object.respond_to?(:errors) ? object : instance_variable_get("@#{object}"))) &&
0
(errors = obj.errors.on(method))
0
@@ -110,30 +117,37 @@ module ActionView
0
# This <tt>DIV</tt> can be tailored by the following options:
0
- # * <tt>header_tag</tt> - Used for the header of the error div (default: h2)
0
- # * <tt>id</tt> - The id of the error div (default: errorExplanation)
0
- # * <tt>class</tt> - The class of the error div (default: errorExplanation)
0
- # * <tt>object</tt> - The object (or array of objects) for which to display errors, if you need to escape the instance variable convention
0
- # * <tt>object_name</tt> - The object name to use in the header, or any text that you prefer. If <tt>object_name</tt> is not set, the name of the first object will be used.
0
- # * <tt>header_message</tt> - The message in the header of the error div. Pass +nil+ or an empty string to avoid the header message altogether. (default: X errors prohibited this object from being saved)
0
- # * <tt>message</tt> - The explanation message after the header message and before the error list. Pass +nil+ or an empty string to avoid the explanation message altogether. (default: There were problems with the following fields:)
0
+ # * <tt>:header_tag</tt> - Used for the header of the error div (default: "h2").
0
+ # * <tt>:id</tt> - The id of the error div (default: "errorExplanation").
0
+ # * <tt>:class</tt> - The class of the error div (default: "errorExplanation").
0
+ # * <tt>:object</tt> - The object (or array of objects) for which to display errors,
0
+ # if you need to escape the instance variable convention.
0
+ # * <tt>:object_name</tt> - The object name to use in the header, or any text that you prefer.
0
+ # If <tt>:object_name</tt> is not set, the name of the first object will be used.
0
+ # * <tt>:header_message</tt> - The message in the header of the error div. Pass +nil+
0
+ # or an empty string to avoid the header message altogether. (Default: "X errors
0
+ # prohibited this object from being saved").
0
+ # * <tt>:message</tt> - The explanation message after the header message and before
0
+ # the error list. Pass +nil+ or an empty string to avoid the explanation message
0
+ # altogether. (Default: "There were problems with the following fields:").
0
- # To specify the display for one object, you simply provide its name as a parameter. For example, for the +User+ model:
0
+ # To specify the display for one object, you simply provide its name as a parameter.
0
+ # For example, for the <tt>@user</tt> model:
0
# error_messages_for 'user'
0
- # To specify more than one object, you simply list them; optionally, you can add an extra +object_name+ parameter, which
0
- # will be the name used in the header message.
0
+ # To specify more than one object, you simply list them; optionally, you can add an extra <tt>:object_name</tt> parameter, which
0
+ # will be the name used in the header message:
0
# error_messages_for 'user_common', 'user', :object_name => 'user'
0
- # If the objects cannot be located as instance variables, you can add an extra +object+ paremeter which gives the actual
0
- # object (or array of objects to use)
0
+ # If the objects cannot be located as instance variables, you can add an extra <tt>:object</tt> paremeter which gives the actual
0
+ # object (or array of objects to use):
0
# error_messages_for 'user', :object => @question.user
0
# NOTE: This is a pre-packaged presentation of the errors with embedded strings and a certain HTML structure. If what
0
- # you need is significantly different from the default presentation, it makes plenty of sense to access the
object.errors0
+ # you need is significantly different from the default presentation, it makes plenty of sense to access the
<tt>object.errors</tt>0
# instance yourself and set it up. View the source of this method to see how easy it is.
0
def error_messages_for(*params)
0
options = params.extract_options!.symbolize_keys