Rails collection_select 如何正确引用哈希?

Leg*_*ack 1 forms arrays hash ruby-on-rails collection-select

正如您在下面看到的,我创建了一个哈希,但我不知道在我的 collection_select 标记中引用该哈希。所以我已经成功地做到了这一点,但我的哈希是配置文件对象的集合,当我尝试使用键值对的集合来执行此操作时,它似乎不起作用,我将首先向您展示正确工作的代码,然后我将向您展示不起作用的代码。

这给了我零错误:

  <% listoflos = [] %>
  <% @profiles.each do |profile|  %>
    <% listoflos.push(profile) if profile.title == "loan officer" %>
  <% end %>
  <%= f.collection_select :loanofficer_id, listoflos, :user_id, :firstname, {prompt: true} %>
Run Code Online (Sandbox Code Playgroud)

这给了我错误:

  <%= f.label "Progress" %>&nbsp
  <% listofprogress = [["1 Not contacted", "1"],["2 Interested", "2"],["3 App Taken", "3"],["4 Priced", "4"],["5 Disclosure Signed", "5"],["6 No Appraisal Needed", "6"],["7 Appraisal Ordered", "7"],["8 Appraisal Recieved", "8"],["9 In Underwriting", "9"],["10 Closing Scheduled", "10"],["11 Closed", "11"],["12 Dead", "12"],["Unknown", "unknown"]] %>

    <%= f.collection_select :progress, listofprogress, :id, :value, {prompt: true} %>
Run Code Online (Sandbox Code Playgroud)

我收到错误:

Records#edit 中的 NoMethodError 显示 c:/Sites/TeamCRM/app/views/records/_eform.html.erb 其中第 52 行引发:

["1 未联系", "1"]:Array 的未定义方法 `value'

你知道我做错了什么吗?

mes*_*jah 5

来自 Rails 文档

collection_select(对象、方法、集合、value_method、text_method、选项 = {}、html_options = {})

: value_method和: text_method参数是要在集合的每个成员上调用的方法。

您的代码尝试调用value一个数组,但它不响应该方法。

尝试使用options_for_select

<%= f.label "Progress" %>
<% listofprogress = [["1 Not contacted", "1"],["2 Interested", "2"],["3 App Taken", "3"],["4 Priced", "4"],["5 Disclosure Signed", "5"],["6 No Appraisal Needed", "6"],["7 Appraisal Ordered", "7"],["8 Appraisal Recieved", "8"],["9 In Underwriting", "9"],["10 Closing Scheduled", "10"],["11 Closed", "11"],["12 Dead", "12"],["Unknown", "unknown"]] %>

<%= f.select :progress, options_for_select(listofprogress, @record.progress_id.to_s), {prompt: true} %>
Run Code Online (Sandbox Code Playgroud)