hellomello
hellomello

Reputation: 8597

Ruby on Rails: putting class with submit_tag

I was wondering why we have to add a nil when putting :class => "class_name"

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

but for this:

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

I don't need to add the nil

Thanks

Upvotes: 10

Views: 17128

Answers (5)

Mandeep Singh
Mandeep Singh

Reputation: 1003

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

First parameter is required and it would be value and they any parameter you want to specify, can be done in a hash like :key=>"value".

Upvotes: 14

LisaD
LisaD

Reputation: 2294

Because they are two different methods...

The "submit" method doesn't take a caption because it can infer one from the form that the method is called on, and what object was used to build the form.

The "submit_tag" method is not called on a form object. It is used for more customized form building (more separated from your activerecord model, for example) and so the code can't infer a caption and must get a value as the first argument. All the "formelement_tag" methods (documented here, for example) are like this and can infer less based on your data model.

Upvotes: 1

Felix
Felix

Reputation: 801

The _tag series of methods usually require a name parameter (otherwise they'd be fairly useless tags, so it's always the first argument instead of part of the hash. Because the submit helper is called as part of the form, Rails can assume the field's name property and can then make the options hash the first argument.

Upvotes: 0

Lu&#237;s Ramalho
Lu&#237;s Ramalho

Reputation: 10218

A look to the way that 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 is value which by default is "Save changes" and the second is a Hash of options. If you don't pass nil then it will assume that that's the value you want for the input.

Upvotes: 9

Jason Kim
Jason Kim

Reputation: 19061

Obvious answer is that submit_tag and submit are simply different form helper methods that takes different arguments.

Upvotes: 0

Related Questions