Codejoy
Codejoy

Reputation: 3826

options_from_collection_for_select error

I am trying to post all the group locations in my table of group locations. The thing is I don't want to post the id's in the select tag I want the names to show up. I have tried several iterations and nothing works. Instead of posting all the code here I already have the relevant code and error in a gist on github:

https://gist.github.com/codejoy/19be7b182e372dd98e76

There error is posted there... basically I just want what I thought was something simple (even if my models are a bit complex in their relationships). I just cannot figure out what this error is telling me, if i switch this line:

<%= select_tag "group_providers", options_from_collection_for_select(GroupLocation.all, 'id' ,@group_locations.map{ |j| j.dba }), :multiple => true%>

To this:

<%= select_tag "group_providers", options_from_collection_for_select(GroupLocation.all, 'id' ,@group_locations.map{ |j| j.id }), :multiple => true%>

The error changes to this:

TypeError in Providers#add_location Showing /vagrant/ipanmv2/app/views/providers/add_location.html.erb where line

51 raised:

[2] is not a symbol nor a string Extracted source (around line #51): 48 49 50 51 52 53 54

             <tr>
               <td>          
                 <% if @group_locations.count >0 %>
                   <%= select_tag "group_providers", options_from_collection_for_select(GroupLocation.all, 'id'

,@group_locations.map{ |j| j.id }), :multiple => true%> <% else %> <%= select_tag "group_providers", "Add new...".html_safe, :multiple => true, :style => "width: 300px" %>
<% end %>

Rails.root: /vagrant/ipanmv2

Application Trace | Framework Trace | Full Trace app/views/providers/add_location.html.erb:51:in `_app_views_providers_add_location_html_erb__410977632_87411510' Request

Parameters:

{"id"=>"4"} Toggle session dump Toggle env dump Response

Headers:

None

Upvotes: 0

Views: 452

Answers (1)

jljohnstone
jljohnstone

Reputation: 1230

If you take a look at the documentation for options_from_collection_for_select, you'll see that it expects the third parameter to be the method used for the option text value. Using the map method here returns an array of your group location IDs which is neither a string or a symbol, hence the error.

If you just want to have the option text to be the value of the dba attribute try this instead:

<%= select_tag "group_providers", options_from_collection_for_select(@group_locations, :id, :dba), :multiple => true %>

Upvotes: 2

Related Questions