diff options
author | José Valim <jose.valim@gmail.com> | 2010-01-14 02:02:07 +0100 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-01-14 02:02:07 +0100 |
commit | 9038a4af354fc6edc2d112c273864ada1c44b05b (patch) | |
tree | 2c731893757aaeccd77e3b914890260f72af63fd | |
parent | 363545aa20014c56f6da223acc4a46de5c873143 (diff) | |
download | rails-9038a4af354fc6edc2d112c273864ada1c44b05b.tar.gz rails-9038a4af354fc6edc2d112c273864ada1c44b05b.tar.bz2 rails-9038a4af354fc6edc2d112c273864ada1c44b05b.zip |
Add documentation to f.submit and make scaffold generators use the new shortcut.
-rw-r--r-- | actionpack/lib/action_view/helpers/form_helper.rb | 49 | ||||
-rw-r--r-- | railties/lib/rails/generators/erb/scaffold/templates/_form.html.erb | 6 |
2 files changed, 35 insertions, 20 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 diff --git a/railties/lib/rails/generators/erb/scaffold/templates/_form.html.erb b/railties/lib/rails/generators/erb/scaffold/templates/_form.html.erb index d02028d983..9c19a09616 100644 --- a/railties/lib/rails/generators/erb/scaffold/templates/_form.html.erb +++ b/railties/lib/rails/generators/erb/scaffold/templates/_form.html.erb @@ -8,10 +8,6 @@ </div> <% end -%> <div class="actions"> - <%% if @<%= singular_name %>.new_record? %> - <%%= f.submit 'Create' %> - <%% else %> - <%%= f.submit 'Update' %> - <%% end %> + <%%= f.submit %> </div> <%% end %> |