Created
April 24, 2012 15:56
-
-
Save walterg2/2480970 to your computer and use it in GitHub Desktop.
Hash won't work in option_from_collection_for_select
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <%= form_for(@structure) do |f| %> | |
| <%= f.select :structural_material, options_from_collection_for_select(@materials, :material, :material), :prompt => "Select" %> | |
| <% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Author
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
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.