如何使用simple_form显示用户列表?

时间:2021-12-19 15:57:26

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

当我这样做时:<%= f.association:user,:collection => User.where(:country_id => 1)%>

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

我的下拉列表中填充了这样的行:# :0x0000010b98d170>

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

相反,我想显示一封与用户ID相关联的电子邮件。

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

我还没有找到在使用关联时如何覆盖simple_form的值/内容默认值。

Can anyone help?

有人可以帮忙吗?

thank you, P.

谢谢你,P。

1 个解决方案

#1


18  

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

虽然github上的页面(https://github.com/plataformatec/simple_form)没有说,但我猜它与示例f.input相同:age,:collection => 18..60

You could use :label_method and :value_method:

您可以使用:label_method和: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.

我之前没用过它。请告诉我它是否不起作用。

#1


18  

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

虽然github上的页面(https://github.com/plataformatec/simple_form)没有说,但我猜它与示例f.input相同:age,:collection => 18..60

You could use :label_method and :value_method:

您可以使用:label_method和: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.

我之前没用过它。请告诉我它是否不起作用。