【问题标题】:How to display a list of users with simple_form?如何使用 simple_form 显示用户列表?
【发布时间】:2011-01-26 22:44:20
【问题描述】:

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

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

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

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

谁能帮忙?

谢谢你, P.

【问题讨论】:

  • 问题中的代码示例请使用反引号或四个空格缩进。
  • 您使用的是哪个版本的导轨。我对关联方法不熟悉。
  • 这是来自 simple_form gem 的方法。我正在使用 Rails 3。

标签: ruby-on-rails forms


【解决方案1】:

虽然github上的页面(https://github.com/plataformatec/simple_form)没说,但我猜应该和例子f.input :age, :collection => 18..60一样

您可以使用:label_method:value_method

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

我以前没用过。如果它不起作用,请告诉我。

【讨论】:

  • 效果很好。谢谢。
猜你喜欢
  • 2015-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多