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:

    
'button'%>
'button' %>

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:

'button'%>
'button' %>

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!


Posted

in

, ,

by

Comments

6 responses to “Placing Multiple Button_To on the Same Line – Ruby on Rails”

  1. Alain Avatar

    Thanks a lot! It works, sharing this on my blog!!

    Like

    1. Chris Owens Avatar
      Chris Owens

      No worries Alain, thanks for the feedback.

      Like

  2. […] Placing Multiple Button_To on the Same Line – Ruby on Rails | What I Broke. […]

    Like

  3. trodicaro Avatar

    Awesome, I just used it too to render 5 buttons inline. Thanks!

    Like

  4.  Avatar
    Anonymous

    Thanks, this worked great!

    Like

  5.  Avatar
    Anonymous

    Thank you!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com

%d bloggers like this: