Rails选择表单助手不能很好地使用我的哈希

时间:2014-07-06 17:20:01

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

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

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

@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> -->

1 个答案:

答案 0 :(得分:1)

它应该是这样的:

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

请注意,第二个参数应该是hasharray of objects,可以调用第3和第4个参数中提供的方法。