# Button\_to generates DIV

**URL:** <https://www.ruby-forum.com/t/button-to-generates-div/49450>\
**Category:** Rails\
**Created:** [November 17, 2005, 2:50pm UTC](https://www.ruby-forum.com/t/button-to-generates-div/49450 "2005-11-17T14:50:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [November 17, 2005, 2:50pm UTC](https://www.ruby-forum.com/t/button-to-generates-div/49450/1 "2005-11-17T14:50:06Z")

</div>

Hi,

I just noticed that button\_to generates a div inside the form element.  
Why?

Jeroen

---

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [November 17, 2005, 10:50pm UTC](https://www.ruby-forum.com/t/button-to-generates-div/49450/2 "2005-11-17T22:50:46Z")

</div>

On 17-nov-2005, at 14:48, Jeroen H. wrote:

> Hi,
> 
> I just noticed that button\_to generates a div inside the form  
> element. Why?

Because it’s a requirement, per XHTML 1.1, to have an enclosing block  
element as an immediate child of the FORM element.

---

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [December 22, 2006, 2:05pm UTC](https://www.ruby-forum.com/t/button-to-generates-div/49450/3 "2006-12-22T14:05:27Z")

</div>

Ugh… That code should read:

ActionView::Base.field\_error\_proc = Proc.new do |html\_tag, instance|  
“\<span class=“error”\>#{html\_tag}”  
end

RSL

---

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [December 22, 2006, 1:37am UTC](https://www.ruby-forum.com/t/button-to-generates-div/49450/4 "2006-12-22T01:37:56Z")

</div>

hey guys, to fix this, i just hacked the button\_to and url\_for helpers  
and included it in my application.rb:

module ActionView  
module Helpers  
module UrlHelper  
def button\_to(name, options = {}, html\_options = nil)

```
    html_options = (html_options || {}).stringify_keys
    convert_boolean_attributes!(html_options, %w( disabled ))

    if confirm = html_options.delete("confirm")
      html_options["onclick"] = "return 

```

#{confirm\_javascript\_function(confirm)};"  
end  
options = {:only\_path=\>false}.update(options)  
url = options.is\_a?(String) ? options : url\_for(options)  
name ||= url

```
    html_options.symbolize_keys!
    tag(:input, html_options.merge({
      :type => "button", :value => name,
      :onclick => (html_options[:onclick] ? 

```

"#{html\_options[:onclick]}; " : “”) +  
“window.location.href=’#{url\_for(options)}’;”  
}))

```
  end

  def url_for(options = {}, *parameters_for_method_reference)
    if options.kind_of? Hash
      options = { :only_path => true 

```

}.update(options.symbolize\_keys)  
escape = options.key?(:escape) ? options.delete(:escape) :  
true  
else  
escape = true  
end  
url = @controller.send(:url\_for, options,  
\*parameters\_for\_method\_reference)  
escape ? html\_escape(url).gsub(/&/,"&") : url # replaces  
the ‘&’ with its original &  
end

```
end

```

end  
end

---

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [July 9, 2007, 9:31pm UTC](https://www.ruby-forum.com/t/button-to-generates-div/49450/5 "2007-07-09T21:31:44Z")

</div>

This is a pet peeve of mine in Rails - there are a few places where  
these div’s pop up inappropriately IMHO. Any idea why?

On Dec 21 2006, 8:37 pm, Aryk G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [December 22, 2006, 2:03pm UTC](https://www.ruby-forum.com/t/button-to-generates-div/49450/6 "2006-12-22T14:03:25Z")

</div>

You might be interested in this snippet of code I saw somewhere and use  
in  
all my Rails apps.

ActionView::Base.field\_error\_proc = Proc.new do |html\_tag, instance|  
“\<span class=“error”\>#{html\_tag}”  
end

That fieldWithError div [a block element] around bad form fields can  
really  
wreck a layout sometimes. An aptly named span works just as well.

RSL

---

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [December 22, 2006, 2:15pm UTC](https://www.ruby-forum.com/t/button-to-generates-div/49450/7 "2006-12-22T14:15:28Z")

</div>

Excellent. I just had that wreck a layout yesterday. Including this code  
should be easier and cleaner than hacking up the CSS.

---

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [November 18, 2005, 12:09am UTC](https://www.ruby-forum.com/t/button-to-generates-div/49450/8 "2005-11-18T00:09:45Z")

</div>

Julian ‘Julik’ Tarkhanov wrote:

> element as an immediate child of the FORM element.  
> Wow. I wonder why they did that… I guess div is a good choice then.

It is a bit of a bummer that we can’t specify say a template for these  
things, I mean the div is hardcoded, you can only change it if you  
overwrite the button\_to method altogether. Are there plans for something  
like this?

Jeroen
