1

正如您在下面看到的,我创建了一个哈希,但我不知道在我的 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} %>

这给了我错误:

  <%= 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} %>

我收到一个错误:

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

["1 Not contacted", "1"]:Array 的未定义方法“值”

你知道我做错了什么吗?

4

1 回答 1

4

来自 Rails 文档

collection_select(对象、方法、集合、value_method、text_method、options = {}、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} %>
于 2015-03-05T18:18:10.763 回答