aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers/form_helper.rb
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2010-01-14 02:02:07 +0100
committerJosé Valim <jose.valim@gmail.com>2010-01-14 02:02:07 +0100
commit9038a4af354fc6edc2d112c273864ada1c44b05b (patch)
tree2c731893757aaeccd77e3b914890260f72af63fd /actionpack/lib/action_view/helpers/form_helper.rb
parent363545aa20014c56f6da223acc4a46de5c873143 (diff)
downloadrails-9038a4af354fc6edc2d112c273864ada1c44b05b.tar.gz
rails-9038a4af354fc6edc2d112c273864ada1c44b05b.tar.bz2
rails-9038a4af354fc6edc2d112c273864ada1c44b05b.zip
Add documentation to f.submit and make scaffold generators use the new shortcut.
Diffstat (limited to 'actionpack/lib/action_view/helpers/form_helper.rb')
-rw-r--r--actionpack/lib/action_view/helpers/form_helper.rb49
1 files changed, 34 insertions, 15 deletions
diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb
index 11ac3ad6f3..cd805061be 100644
--- a/actionpack/lib/action_view/helpers/form_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_helper.rb
@@ -1077,23 +1077,28 @@ module ActionView
@template.error_messages_for(@object_name, objectify_options(options))
end
+ # Add the submit button for the given form. When no value is given, it checks
+ # if the object is a new resource or not to create the proper label:
+ #
+ # <% form_for @post do %>
+ # <%= f.submit %>
+ # <% end %>
+ #
+ # In the example above, if @post is a new record, it will use "Create Post" as
+ # submit button label, otherwise, it uses "Update Post".
+ #
+ # Those labels can be customized using I18n, under the helpers.submit key and accept
+ # the {{model}} as translation interpolation:
+ #
+ # en:
+ # helpers:
+ # submit:
+ # create: "Create a {{model}}"
+ # update: "Confirm changes to {{model}}"
+ #
def submit(value=nil, options={})
value, options = nil, value if value.is_a?(Hash)
-
- unless value
- object = @object.respond_to?(:to_model) ? @object.to_model : @object
- key = object ? (object.new_record? ? :create : :update) : :submit
-
- model = if object.class.respond_to?(:model_name)
- object.class.model_name.human
- else
- @object_name.to_s.humanize
- end
-
- value = I18n.t(:"helpers.submit.#{key}", :model => model,
- :default => "#{key.to_s.humanize} #{model}")
- end
-
+ value ||= submit_default_value
@template.submit_tag(value, options.reverse_merge(:id => "#{object_name}_submit"))
end
@@ -1106,6 +1111,20 @@ module ActionView
@default_options.merge(options.merge(:object => @object))
end
+ def submit_default_value
+ object = @object.respond_to?(:to_model) ? @object.to_model : @object
+ key = object ? (object.new_record? ? :create : :update) : :submit
+
+ model = if object.class.respond_to?(:model_name)
+ object.class.model_name.human
+ else
+ @object_name.to_s.humanize
+ end
+
+ I18n.t(:"helpers.submit.#{key}", :model => model,
+ :default => "#{key.to_s.humanize} #{model}")
+ end
+
def nested_attributes_association?(association_name)
@object.respond_to?("#{association_name}_attributes=")
end