【问题标题】:collection_select does not display text_method in rails 4collection_select 在 rails 4 中不显示 text_method
【发布时间】:2016-02-02 03:42:42
【问题描述】:

我正在构建一个小项目,但它出错了。 我想使用一个选择框来选择我的地区(地区模型有 2 列:id、name) 这是我的代码

 <%= f.select :district_id, options_for_select(District.all.collect {|p| [ p.name, p.id ] }), {prompt: 'Select District'}, class: "form-control" %>

但是当我运行它时,没有显示地区的名称。

我的数据库中记录区的数量是 9 条记录。可以看到选项的数量是正确的,但是区名消失了。

我的地区表

更新:我已通过使用成功修复了我的错误

 <%= f.select :district_id, options_for_select(District.all.collect {|p| [ p[:name], p.id ] }), {prompt: 'Select District'}, class: "form-control" %>

我刚刚更改了 District.name => District[:name] 谁能为我解释一下。 谢谢大家。

【问题讨论】:

  • 你确定名字不为空吗?
  • 试试&lt;%= f.select :district_id, District.all.collect {|p| [ p.name, p.id ] }, {prompt: 'Select District'}, class: "form-control" %&gt;
  • @Pavan 同样的结果@@
  • 试试这个

标签: ruby-on-rails ruby ruby-on-rails-4 select collection-select


【解决方案1】:
<%= f.collection_select :district_id, District.all, :id, :name, {prompt: 'Select District'}, class: "form-control" %>

【讨论】:

  • 谢谢,我试过了,还是一样的错误。
  • 我试过你的建议,但仍然没有显示区名。
  • 请考虑编辑您的帖子,以添加更多关于您的代码的作用以及它为何能解决问题的说明。一个大部分只包含代码的答案(即使它正在工作)通常不会帮助 OP 理解他们的问题。
猜你喜欢
  • 2016-04-02
  • 1970-01-01
  • 2016-08-16
  • 2021-10-04
  • 2016-07-31
  • 2016-11-22
  • 2016-06-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多