Skip to content

Instantly share code, notes, and snippets.

@walterg2
Created April 24, 2012 15:56
Show Gist options
  • Save walterg2/2480970 to your computer and use it in GitHub Desktop.
Save walterg2/2480970 to your computer and use it in GitHub Desktop.
Hash won't work in option_from_collection_for_select
<%= form_for(@structure) do |f| %>
<%= f.select :structural_material, options_from_collection_for_select(@materials, :material, :material), :prompt => "Select" %>
<% end %>
class Structure < ActiveRecord::Base
validates :year_home_built, :presence => true
validates :year_home_purchased, :presence => true
validates :structural_material, :presence => true
validates :roof_style, :presence => true
validates :reconstruction_cost, :presence => true
@@materials_list = [{
material: 'Brick'
},
{
material: 'Hardiplank'
},
{
material: 'Stone'
},
{
material: 'Stucco'
},
{
material: 'Vinyl'
},
{
material: 'Wood'
}]
def self.fetch_materials
@@materials_list
end
end
class StructuresController < ApplicationController
# GET /structures/new
# GET /structures/new.json
def new
@structure = Structure.new
@materials = Structure.fetch_materials
logger.debug @materials
respond_to do |format|
format.html # new.html.erb
format.json { render json: @structure }
end
end
end
@walterg2
Copy link
Author

I can get this working if I use only an array and option_for_select, just not options_from_collection_for_select with a hash.

@walterg2
Copy link
Author

Thanks to @mikelikesbikes, my answer was to use OpenStruct objects instead of standard Hashes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment