Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a list of users with simple_form?

When I do this: <%= f.association :user, :collection => User.where(:country_id => 1) %>

My dropdown is populated with lines like this: #<User:0x0000010b98d170>

Instead, I would like to display an email, that is tied to the id of the users.

I have yet to find how to override the value / content defaults of simple_form when using associations.

Can anyone help?

thank you, P.

like image 772
Pierre Avatar asked Jan 26 '11 22:01

Pierre


Video Answer


1 Answers

Although the page on github (https://github.com/plataformatec/simple_form) didn't say, but I guess it's the same as the example f.input :age, :collection => 18..60

You could use :label_method and :value_method:

f.association :user, :collection => User.where(:country_id => 1), :label_method => :name, :value_method => :id

I did not use it before. Please tell me if it does not work.

like image 186
PeterWong Avatar answered Nov 15 '22 06:11

PeterWong