0

I have a form using slim/simple_form.

= f.association :users,
  collection: @users,
  prompt: "Choose Recipient",
  label_method: :first_name

I would like to have the user's full name as the label method, could someone point me in the right direction? I only have first_name and last_name as attributes. I will also prefer to add the user's company name next to their full name for clarity in selection. Does simple form allow this? else I will retreat to options from collection for select

2 Answers 2

1

Create a method on your model:

class User < ArcitveRecord::Base
  # ...

  def full_name
    "#{first_name} #{last_name}"
  end
end

and use it:

= f.association :users,
  collection: @users,
  prompt: "Choose Recipient",
  label_method: :full_name
Sign up to request clarification or add additional context in comments.

1 Comment

I actually already had it in the model. Silly me didn't think to use it, I thought only database attributes were usable
1

If you like to prefer adding company_name next to the full_name,I would just modify Broisatse's answer as

class User < ArcitveRecord::Base
  # ...

  def full_name_with_company_name
    "#{first_name} #{last_name} #{company_name}"
  end
end

and use it:

= f.association :users,
  collection: @users,
  prompt: "Choose Recipient",
  label_method: :full_name_with_company_name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.