almo
almo

Reputation: 6367

Labels of checkboxes of Rails form association

In my simple form I have an association to select a list of groups.

<%= f.association :groups, as: :check_boxes, :include_hidden => false, label: 'Grupos' %>

This creates me a list of checkboxes, each with the label from the name of he group.

I also have a relation that a group belongs to a company. And I would like to add the company name to the label of the checkboxes.

So right now each checkbox has a label:

Group Name

And I want it to be

Group Name (Company Name)

So basically I am looking for a way to customize the label of the checkboxes created with f.association.

Upvotes: 0

Views: 205

Answers (1)

Ronan Lopes
Ronan Lopes

Reputation: 3398

You can use the label_method argument as, for example:

<%= f.association :groups, as: :check_boxes, :label_method => lambda { |group| "#{group.name} (#{group.company.name})", :include_hidden => false, label: 'Grupos' %>

In that case, I'm assuming that the values you wanna display are on name attribute. If not, you can just adapt it. Hope it helps!

Upvotes: 1

Related Questions