Placing Multiple Button_To on the Same Line – Ruby on Rails

This is a problem that had me confused for an embarrassingly long time. Rails wraps button_to elements within a form and a div. Unfortunately this will take up 100% of the available width. Thankfully the solution is pretty straight forward – simply wrap buttons in a div and float them left like so:

1
2
3
4
5
6
    <div style="float:left;">
      <%= f.submit 'Place Order', :class => 'button'%>
    </div>
    <div style="float:left;">
      <%= button_to 'Cancel', :root, :class => 'button' %>
    </div>

A wrapper div isn’t strictly necessary however I usually add one just to be safe. While this will probably give a bunch of designers another reason to hate developers, I find that wrapping floated elements seems to save a lot of headaches when making changes down the track.

Using the code above you may also find that the divs aren’t filling as expected, simply add a div with a ‘clear:both’ style to the bottom of the wrapper:

1
2
3
4
5
6
7
8
9
10
<div>
    <div style="float:left;">
      <%= f.submit 'Place Order', :class => 'button'%>
    </div>
    <div style="float:left;">
      <%= button_to 'Cancel', :root, :class => 'button' %>
    </div>
    <div style="clear:both;">
    </div>
</div>

Hopefully that doesn’t take anywhere near as long for you guys to figure out as it did me, time for a coffee break I think. Good luck!

5 thoughts on “Placing Multiple Button_To on the Same Line – Ruby on Rails

  1. Pingback: Placing Multiple Button_To on the Same Line – Ruby on Rails | What I Broke | Wildeng TechDad

Leave a Reply