public
Description: Repository for improving Rails documentation
Homepage: http://www.rubyonrails.org
Clone URL: git://github.com/lifo/doc-rails.git
Search Repo:
revised documentation of active_record_helper.rb
Xavier Noria (author)
Sun Apr 20 12:24:52 -0700 2008
commit  30882b0abf7441e3ca9c35c7ec0218f2ac61a5f9
tree    9367adbf887903ebbbf0fb76a58202f2fe6d3076
parent  8f47958d0ae15a046d3392b62042bd5500824258
...
8
9
10
11
 
12
13
14
 
15
16
17
18
19
 
 
 
 
 
20
21
22
23
24
25
 
 
 
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
42
 
43
44
45
 
 
 
 
 
46
47
48
49
50
51
 
52
 
 
 
 
 
 
53
54
55
...
59
60
61
62
63
64
65
66
 
 
 
 
 
67
68
69
70
71
72
...
84
85
86
87
88
 
89
90
91
 
 
92
93
94
 
 
95
96
97
 
 
98
99
100
101
102
103
104
...
110
111
112
113
114
115
116
117
118
119
 
 
 
 
 
 
 
 
 
 
 
 
 
120
121
 
 
122
123
124
125
126
 
 
127
128
129
130
131
 
 
132
133
134
135
136
 
137
138
139
...
8
9
10
 
11
12
13
 
14
15
 
 
 
 
16
17
18
19
20
21
22
23
24
 
 
25
26
27
28
 
 
 
 
 
 
 
 
 
 
 
 
 
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
 
45
46
 
 
47
48
49
50
51
52
 
 
 
 
 
53
54
55
56
57
58
59
60
61
62
63
...
67
68
69
 
 
 
 
 
70
71
72
73
74
75
76
77
78
79
80
...
92
93
94
 
 
95
96
 
 
97
98
99
 
 
100
101
102
 
 
103
104
105
106
107
108
109
110
111
...
117
118
119
 
 
 
 
 
 
 
120
121
122
123
124
125
126
127
128
129
130
131
132
133
 
134
135
136
137
138
 
 
139
140
141
142
143
 
 
144
145
146
147
148
149
 
150
151
152
153
0
@@ -8,48 +8,56 @@
0
   end
0
 
0
   module Helpers
0
- # The Active Record Helper makes it easier to create forms for records kept in instance variables. The most far-reaching is the form
0
+ # 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
+ #
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
       end
0
 
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
+ #
0
       # form("post")
0
- # That line would yield a form like the following:
0
- # <form action='/post/create' method='post'>
0
- # <p>
0
- # <label for="post_title">Title</label><br />
0
- # <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />
0
- # </p>
0
- # <p>
0
- # <label for="post_body">Body</label><br />
0
- # <textarea cols="40" id="post_body" name="post[body]" rows="20">
0
- # </textarea>
0
- # </p>
0
- # <input type='submit' value='Create' />
0
- # </form>
0
       #
0
+ # would yield a form like the following (modulus formatting):
0
+ #
0
+ # <form action='/posts/create' method='post'>
0
+ # <p>
0
+ # <label for="post_title">Title</label><br />
0
+ # <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />
0
+ # </p>
0
+ # <p>
0
+ # <label for="post_body">Body</label><br />
0
+ # <textarea cols="40" id="post_body" name="post[body]" rows="20"></textarea>
0
+ # </p>
0
+ # <input name="commit" type="submit" value="Create" />
0
+ # </form>
0
+ #
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
       #
0
- # form("entry", :action => "sign", :input_block =>
0
- # Proc.new { |record, column| "#{column.human_name}: #{input(record, column.name)}<br />" }) =>
0
+ # form("entry",
0
+ # :action => "sign",
0
+ # :input_block => Proc.new { |record, column|
0
+ # "#{column.human_name}: #{input(record, column.name)}<br />"
0
+ # })
0
       #
0
- # <form action='/post/sign' method='post'>
0
- # Message:
0
- # <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" /><br />
0
- # <input type='submit' value='Sign' />
0
- # </form>
0
+ # would yield a form like the following (modulus formatting):
0
       #
0
+ # <form action="/entries/sign" method="post">
0
+ # Message:
0
+ # <input id="entry_message" name="entry[message]" size="30" type="text" /><br />
0
+ # <input name="commit" type="submit" value="Sign" />
0
+ # </form>
0
+ #
0
       # It's also possible to add additional content to the form by giving it a block, such as:
0
       #
0
       # form("entry", :action => "sign") do |form|
0
@@ -59,11 +67,11 @@
0
       #
0
       # The following options are available:
0
       #
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
 
0
0
0
0
@@ -84,17 +92,16 @@
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
       #
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
       #
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
       #
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
0
0
0
0
@@ -110,30 +117,37 @@
0
       #
0
       # This <tt>DIV</tt> can be tailored by the following options:
0
       #
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
       #
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
       #
0
       # error_messages_for 'user'
0
       #
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
       #
0
       # error_messages_for 'user_common', 'user', :object_name => 'user'
0
       #
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
       #
0
       # error_messages_for 'user', :object => @question.user
0
       #
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.errors
0
+ # 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

Comments

    No one has commented yet.