1

As you can see below I have created a hash but I don't know to to reference that hash in my collection_select tag. So I already did this successfully but my hash was a collection of profile objects, when I try to do it with a collection of key value pairs it doesn't seem to work, I'll show you the code that worked correctly first then I'll show you the code that didn't work.

THIS GAVE ME ZERO ERRORS:

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

THIS GIVES ME ERROR:

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

I get an error:

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

Do you know what I'm doing wrong?

MetaStack
  • 3,266
  • 4
  • 30
  • 67

1 Answers1

5

From the Rails docs

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.

Your code is trying to call value on an Array, which doesn't respond to that method.

Try using 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} %>
messanjah
  • 8,977
  • 4
  • 27
  • 40
  • I previously tried options_for_select, however when using this method it would not automatically select the correct option to match the data on the record.progress option. If @record.progress was set to "3 App Taken" it would ignore that and only come up with the first option when I went to the edit record screen. I even tested it with this appended to the end: {:selected => "3 App Taken"} and it didn't work, even when I tried params[:progress] however it always showed the correct loanofficer_id and that one was using collect_select so I decided to try that. – MetaStack Mar 05 '15 at 18:25
  • `options_for_select` takes a selected value as the 2nd argument. I'll update my answer. – messanjah Mar 05 '15 at 18:32
  • Instead of casting `@record.progress_id` to a string, you could build out `listofprogress` to use integers as identifiers instead. – messanjah Mar 05 '15 at 18:35