【问题标题】:Rails select form helpers not playing nice with my hashRails 选择表单助手不能很好地处理我的哈希
【发布时间】:2014-07-06 17:20:01
【问题描述】:

我想在我的编辑表单中更新Transaction 对象的status 属性。状态应该是一个下拉列表,我可以在其中选择与实际修补到对象的整数值相关的文本值。

换句话说,我有一个看起来像这样的变量:

@statuscodes = [
{ "Working" => 1 },
{ "In progress" => 2 },
{ "Cancelled: No response from borrower" => 3  },
{ "Cancelled: No response from lender" => 4 },
{ "Cancelled: Time period too long" => 5 },
...
]

按照文档和其他一些 SO 帖子,我尝试了 f.selectf.collection_select,我认为这是正确的语法。显然不是,我已经为后面的每一个打印了错误。对我做错了什么有什么想法吗?

<%= f.select :transaction, :status, options_for_select(@statuscodes), {include_blank: true} %>
<!-- undefined method `merge' for #<ActiveSupport::SafeBuffer:0x007fd9da299300> -->

<%= f.collection_select :transaction, :status, @statuscodes, :last, :first, {include_blank: true} %>
<!-- undefined method `merge' for :first:Symbol -->

<%= f.select :transaction, :status, @statuscodes, {include_blank: true} %>
<!-- undefined method `merge' for #<Array:0x007fd9e1381b30> -->

【问题讨论】:

标签: ruby-on-rails forms ruby-on-rails-4 form-helpers


【解决方案1】:

应该是这样的:

<%= f.collection_select :status, @statuscodes[0], :last, :first, {include_blank: true} %>

请注意,第二个参数应该是hasharray of objects,在这些参数上可以调用第三个和第四个参数中提供的方法。

【讨论】:

  • 每个人都是对的...我不需要:transaction,而且我的@statuscodes 被错误地声明了。我把它作为一个单独的哈希数组,因为我猜我累了......而不是一个实际的哈希!
猜你喜欢
  • 1970-01-01
  • 2012-04-25
  • 2010-12-12
  • 2014-03-30
  • 2011-09-08
  • 2011-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多