RAILS, HAML : How to put Checkbox and label on the same line, with translations

Hey guys!

Today I have decided to share with you little secret, that can save you quite some time, while writing templates with HAML.

While creating a form for one of the projects, I needed to put a checkbox within the same line as label and also I had to make my label a “translation”.

I googled and the closest which I was able to find was:

http://stackoverflow.com/questions/9774555/nesting-ruby-on-rails-haml-checkbox-in-label-tag

However, It didn’t really solve my “translation” problem.

Initially my code looked like this:

= simple_form_line do
  = f.label t('.send_email')
  = f.check_box :send_email, class: "checkbox"

.send_email in this case is a translation from my eng.yml file which equals to “Send credentials to user by email”.

The code produced this output:

Image

However, what I needed my checkbox and message to be on the same line. This is how I was able to solve it:

= simple_form_line do
  = f.label t('.send_email')
  = f.check_box :send_email, class: "checkbox"

)

(Sorry if wordpress screwed indentation!!!)

And here is how output looks like:

Image

That’s it for today!

Have a wonderful day!

Regards,

Anatoly

Thanks for installing the Bottom of every post plugin by Corey Salzano. Contact me if you need custom WordPress plugins or website design.

Anatoly Spektor

IT Consultant with 6 years experience in Software Development and IT Leadership. Participated in such projects as Eclipse IDE, Big Blue Button, Toronto 2015 Panam Games.

Join the Discussion

Your email address will not be published. Required fields are marked *

arrow