james
james

Reputation: 4049

Check_box_tag not displaying labels properly

Everything is posting correctly, but I do not see any labels in my checkboxes, just blanks. My form looks like this:

          <%= form_for @itemrecord do |f| %>

            <div class="col-xs-12">
              <p><b>Items people are asking for</b></p>
            </div>

            <% @wishlist.each do |category, list| %>
              <div class="col-xs-2">
                <div class="form-group box">
                  <h5> <%="#{category}"%> </h5>
                    <% list.each do |thing| %>
                        <%= check_box_tag ":item_name[]", "#{thing}" %>
                      </br>
                    <% end %>
                </div>
              </div>  
            <% end %>

              <%= f.submit "Go!", class: "btn btn-primary btn-large btn-block" %>
            </div>

          <% end %>

What's happening is that wishlist is a hash of categories and items within those categories that I set in the controller, and is then called by multiple form builders to build checkboxes. The challenge is that right now in the current implementation, the checkboxes params are passed through properly (FYI controller code is at the bottom), but beside each checkbox, there is no text that shows the thing (i.e., no label so that people know what they're checking.

Here's the html generated for one checkbox (it's the same for all checkboxes)

Basically, I need to make the value the label.


FYI what's happening is that for every item checked, a record is being created. Here's the controller code:

def create

    items_to_be_saved = []
    inventory_params.each do |i|
      items_to_be_saved << ({ :signup_id =>    Signup.find_by_email(session[:signup_email]).id, :item_name => i })
    end

    if Inventory.create items_to_be_saved
      flash[:success] = "Thanks!"
      redirect_to root_path
    else
      render new_inventory_path
    end
end

def inventory_params
  params.require(":item_name")
end

Upvotes: 0

Views: 1005

Answers (2)

Sanket
Sanket

Reputation: 165

can you please try it and let me know

<%= check_box_tag 'item_name[]', thing %>

Upvotes: 0

Airat Shigapov
Airat Shigapov

Reputation: 585

In your code:

<%= check_box_tag ":item_name[]", "#{thing}" %>

Second parameter for check_box_tag is not a label value, it is just a value which goes to controller in parameters. If you wan't to display label within your checkbox you will need to call label_tag in your view:

= label_tag ':item_name[]', thing
= check_box_tag ':item_name[]'

But you definitely should check simple_form gem which allows you to render checkboxes in much cleaner way:

f.input :field, as: :boolean, inline_label: 'Label'

Upvotes: 1

Related Questions