Ruby on Rails: adding a class with submit_tag

I was wondering why we add nilduring installation:class => "class_name"

<%= submit_tag nil, :class => "class_name" %>

but for this:

<%= f.submit class: "class-Name" %>

I do not need to add nil

thank

+5
source share
5 answers

Look at the way the submit_tag method was implemented clearly answers your question.

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

    if disable_with = options.delete("disable_with")
      options["data-disable-with"] = disable_with
    end

    if confirm = options.delete("confirm")
      options["data-confirm"] = confirm
    end

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

It takes two arguments, the first one value, which by default is Save Changes, and the second is the Hash option. If you fail nil, then it will assume that it is a value for input.

+8
source
     <%= submit_tag("Update", :id=>"button", :class=>"Test", :name=>"submit") %>

, , , , , : key = > "value".

+7

...

"submit" , , , .

"submit_tag" . (, activerecord), . "formelement_tag" ( , ) .

+1

For a series of _tag methods, a name parameter is usually required (otherwise they would be useless tags, so it is always the first argument, not part of the hash. Since the view helper is called part of the form, Rails can assume the field name property and then make the hash parameters the first argument.

0
source

The obvious answer is that submit_tagand submitare just different helper form methods that take different arguments.

-1
source

All Articles