public Method

FormTagHelper.submit_tag(value = "Save changes", options = {})

Contents:

Creates a submit button with the text value as the caption.

Options

  • :disabled - If set to true, the user will not be able to use this input.
  • :disable_with - Value of this parameter will be used as the value for a disabled version
    of the submit button when the form is submitted.
  • Any other key creates standard HTML options for the tag.

Examples

submit_tag
# => <input name="commit" type="submit" value="Save changes" />

submit_tag "Edit this article"
# => <input name="commit" type="submit" value="Edit this article" />

submit_tag "Save edits", :disabled => true
# => <input name="commit" type="submit" disabled="disabled" value="Save edits" />

submit_tag "Complete sale", :disable_with => "Please wait..."
# => <input name="commit" onclick="this.disabled=true;this.value='Please wait...';this.form.submit();"
#    type="submit" value="Complete sale" />

submit_tag nil, :class => "form_submit"
# => <input name="commit" type="submit" />

submit_tag "Edit", :disable_with => "Editing...", :class => 'edit-button'
# => <input name="commit" type="submit" value="Edit" disable_with="Editing..." />

Source Code

# File action_view/helpers/form_tag_helper.rb, line 343
def submit_tag(value = "Save changes", options = {})
  options.stringify_keys!

  if disable_with = options.delete("disable_with")
    options["onclick"] = [
      "this.setAttribute('originalValue', this.value)",
      "this.disabled=true",
      "this.value='#{disable_with}'",
      "#{options["onclick"]}",
      "result = (this.form.onsubmit ? (this.form.onsubmit() ? this.form.submit() : false) : this.form.submit())",
      "if (result == false) { this.value = this.getAttribute('originalValue'); this.disabled = false }",
      "return result",
    ].join(";")
  end

  tag :input, { "type" => "submit", "name" => "commit", "value" => value }.update(options.stringify_keys)
end
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.