Rails collection_select 如何正确引用hash?
Rails collection_select how to reference hash correctly?
正如您在下面看到的,我创建了一个散列,但我不知道要在我的 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" %> 
<% 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 in Records#edit Showing
c:/Sites/TeamCRM/app/views/records/_eform.html.erb where line #52
raised:
undefined method `value' for ["1 Not contacted", "1"]:Array
你知道我做错了什么吗?
来自 Rails 文档
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
The :value_method and :text_method parameters are methods to be called on each member of collection.
您的代码试图在数组上调用 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} %>
正如您在下面看到的,我创建了一个散列,但我不知道要在我的 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" %> 
<% 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 in Records#edit Showing c:/Sites/TeamCRM/app/views/records/_eform.html.erb where line #52 raised:
undefined method `value' for ["1 Not contacted", "1"]:Array
你知道我做错了什么吗?
来自 Rails 文档
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
The :value_method and :text_method parameters are methods to be called on each member of collection.
您的代码试图在数组上调用 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} %>