0

What I'm trying to get is a dropdown with 2 fields (name and account_code) as a value

<option value="1">ACC: John Doe</option>

But current code

<%= select_tag :user_id, options_for_select(@users.map{|u| [u.name, u.account_code] }) %>

outputs the following: <option value="1">John Doe</option>

How do I make it display both user's account_code and name? Thanks!

2 Answers 2

1

Try this:

<%= select_tag :user_id, options_for_select(@users.map{|u| [u&.account_code+" "+u&.name, u.account_code] }) %>

Hope this helps you.

Sign up to request clarification or add additional context in comments.

2 Comments

gives me an error #<NoMethodError: undefined method `+' for nil:NilClass>
unfortunately I got the same error. Though it worked perfectly like this @users.map{|u| ["#{u.account_code}: #{u.name}", u.account_code] } Thanks for pointing the direction!
0

Worked like this:

@users.map{|u| ["#{u.account_code}: #{u.name}", u.account_code] }

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.