# How to prevent a newline after a button\_to

**URL:** <https://www.ruby-forum.com/t/how-to-prevent-a-newline-after-a-button-to/158909>\
**Category:** Rails\
**Created:** [January 29, 2009, 10:27am UTC](https://www.ruby-forum.com/t/how-to-prevent-a-newline-after-a-button-to/158909 "2009-01-29T10:27:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [January 29, 2009, 10:27am UTC](https://www.ruby-forum.com/t/how-to-prevent-a-newline-after-a-button-to/158909/1 "2009-01-29T10:27:55Z")

</div>

How do you prevent a newline after a button\_to.

I thought \<%= button\_to blabla -%\> was meant to do it, but it didn’t I  
think I got it generated

 tags around the button code.

How do you do it?

thanks

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [January 29, 2009, 3:17pm UTC](https://www.ruby-forum.com/t/how-to-prevent-a-newline-after-a-button-to/158909/2 "2009-01-29T15:17:06Z")

</div>

On Jan 29, 4:27 am, bingo bob [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> How do you prevent a newline after a button\_to.
> 
> I thought \<%= button\_to blabla -%\> was meant to do it, but it didn’t I  
> think I got it generated
> 
> tags around the button code.
> 
> How do you do it?

This is something that gets tossed around from time to time. The  
official (barely) agreed upon stance is:

"The div is for HTML4.01/XHTML1.0 strict compliance. An input can only  
be contained by one of “p”, “h1”, “h2”, “h3”, “h4”, “h5”, “h6”, “div”,  
“pre”, “address”, “fieldset”, “ins” or “del”. " [  
[http://dev.rubyonrails.org/ticket/10422](http://dev.rubyonrails.org/ticket/10422)  
]

But this annoys me too. There are a few ways of handling this.  
button\_to creates a form tag with a class of “button\_to”. So in your  
stylesheet, you should be able to do something  
with .button\_to .form .div and remove the spacing. (I haven’t tried  
this myself yet, and I’m not a css whiz, so YMMV).

If you care less about strict compliance, and want to get rid of the  
div tags totally, then the following in you application.rb will work:

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
end

```

end
