Rails collection_select如何正确引用哈希?

时间:2015-03-05 18:11:04

标签: ruby-on-rails arrays forms hash 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} %>

这给了我错误:

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

我收到错误:

  

记录中的NoMethodError编辑显示   c:/Sites/TeamCRM/app/views/records/_eform.html.erb第52行   提出:

     

未定义的方法`值&#39; [&#34; 1未联系&#34;,&#34; 1&#34;]:数组

你知道我做错了吗?

1 个答案:

答案 0 :(得分:3)

来自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} %>