Josh M.
Josh M.

Reputation: 27781

Make simple_form honor my blank/empty label

Using simple_form v2.1.0 in a Rails 3.2.13 app. I have a simple_form where I want to have a checkbox show up with an inline label, and I want to have an "empty" normal label. This way, the label's container should remain in place and the checkbox will be properly positioned with the other form elements.

If I set :label => "" or :label => " " or :label => nil or don't provide a :label at all, I end up with the field name as the label.

If I set :label => false, the label and containing elements aren't rendered at all (so the checkbox appears at the far left of the page).

If I set :label => " ", I end up with the literal text  , as expected.

It seems there is no way to tell simple form to leave the label there, just don't put any text in its container.

Here is an example form where I'm trying to setup an empty label for a checkbox with an inline label.

<%= simple_form_for @model ... %>
    <%= f.input :sticky, :label => "", :inline_label => "Sticky?", :as => :boolean ... %>
<% end %>

But simple_form thinks it's smarter than me and defaults the label. I wish it only did that if I hadn't specifically supplied a label!

Simple form doesn't allow blank labels!

Upvotes: 4

Views: 1345

Answers (3)

SoyTeins
SoyTeins

Reputation: 11

Was having similar troubles. In your form defaults just do this:

defaults: { :label => 'blanklabel', label_html: { class: 'blanklabel'}

Then in your CSS, just set the label text color to white:

.blanklabel { color: white; }

Upvotes: 0

Ahmad Sherif
Ahmad Sherif

Reputation: 6223

Try setting :label value to false.

UPDATE: Since SimpleForm is supposed to rely on ActionView to render the elements, you can use this hackish solution: set the label in your locale file to empty string, like this

en:
  activerecord:
    attributes:
      model_name_underscored:
        sticky: ' '

Upvotes: 1

Kieran Andrews
Kieran Andrews

Reputation: 5885

Have you tried removing the :label attribute in your input completely and instead using form.label for the empty label.

http://apidock.com/rails/ActionView/Helpers/FormHelper/label

Instead of using an empty label you could position your check box with CSS.

Upvotes: 0

Related Questions