Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String concatenation not possible?

So over the last 2 hours, I've been trying to fill a combobox with all my users. I managed to get all firstnames in a combobox, but I want their full name in the combobox. No problem you would think, just concatenate the names and you're done. + and << should be the concatenation operator to do this.So this is my code:

<%= collection_select(:user, :user_id, @users, :user_id, :user_firstname + :user_lastname, {:prompt => false}) %>

But it seems RoR doesn't accept this:

undefined method `+' for :user_firstname:Symbol

What am I doing wrong?

like image 651
Bv202 Avatar asked Dec 02 '22 02:12

Bv202


1 Answers

What you need to do is define a method on the User model that does this concatenation for you. Symbols can't be concatenated. So to your user model, add this function:

def name
  "#{self.first_name} #{self.last_name}"
end

then change the code in the view to this:

<%= collection_select(:user, :user_id, @users, :user_id, :name, {:prompt => false}) %>

Should do the trick.

like image 72
Will Barrett Avatar answered Jan 02 '23 19:01

Will Barrett