Skip to content

Instantly share code, notes, and snippets.

@jonathan
Created October 5, 2009 16:13
Show Gist options
  • Save jonathan/202220 to your computer and use it in GitHub Desktop.
Save jonathan/202220 to your computer and use it in GitHub Desktop.
class Candidate
include MongoMapper::Document
key :alias, String, :required => true
key :notes, String
many :phones
end
class Phone
include MongoMapper::EmbeddedDocument
TYPES = ['Mobile', 'Home', 'Work'].freeze
key :number, String, :required => true
key :type, String, :required => true, :within => TYPES
belongs_to :candidate
def self.types()
TYPES
end
end
# Candidate form
<% form_for(@candidate) do |f| %>
<%= f.error_messages %>
<div id="candidate">
<p>
<%= f.label(:alias, 'Alias') %>
<%= f.text_field(:alias) %>
</p>
<p>
<%= f.label(:notes, 'Notes') %>
<%= f.text_area(:notes) %>
</p>
<div id="phones">
<% f.fields_for :phones do |phone| %>
<p>
<%= phone.label(:type, 'Type') %>
<%= phone.select(:type, Phone.types) %>
</p>
<p>
<%= phone.label(:number, 'Phone Number') %>
<%= phone.text_field(:number) %>
</p>
<% end %>
</div>
</div>
<p>
<%= f.submit 'Save' %>
</p>
<% end %>
# When saved, this is the error:
Processing CandidatesController#update (for 127.0.0.1 at 2009-10-04 19:04:44) [PUT]
Parameters: {"commit"=>"Save →", "id"=>"4ac7a91de23cde111f001002", "candidate"=>{"notes"=>"here is some text for a note", "phones"=>{"number"=>"555-555-5555", "type"=>"Mobile"}, "alias"=>"huskies"}}
NoMethodError (undefined method each_pair' for ["number", "555-555-5555"]:Array):<br/> mongomapper (0.4.1) lib/mongomapper/embedded_document.rb:209:inattributes=' mongomapper (0.4.1) lib/mongomapper/embedded_document.rb:186:in initialize' mongomapper (0.4.1) lib/mongomapper/associations/many_embedded_proxy.rb:46:innew' mongomapper (0.4.1) lib/mongomapper/associations/many_embedded_proxy.rb:46:in find_target' mongomapper (0.4.1) lib/mongomapper/associations/many_embedded_proxy.rb:45:inmap' mongomapper (0.4.1) lib/mongomapper/associations/many_embedded_proxy.rb:45:in find_target' mongomapper (0.4.1) lib/mongomapper/associations/proxy.rb:48:inload_target' mongomapper (0.4.1) lib/mongomapper/associations/proxy.rb:38:in method_missing' mongomapper (0.4.1) lib/mongomapper/embedded_document.rb:251:into_mongo' mongomapper (0.4.1) lib/mongomapper/embedded_document.rb:249:in each' mongomapper (0.4.1) lib/mongomapper/embedded_document.rb:249:into_mongo' mongomapper (0.4.1) lib/mongomapper/document.rb:328:in save_to_collection' mongomapper (0.4.1) lib/mongomapper/document.rb:323:inupdate_without_callbacks' mongomapper (0.4.1) lib/mongomapper/callbacks.rb:49:in update' mongomapper (0.4.1) lib/mongomapper/document.rb:307:increate_or_update_without_callbacks' mongomapper (0.4.1) lib/mongomapper/callbacks.rb:25:in create_or_update' mongomapper (0.4.1) lib/mongomapper/document.rb:290:insave_without_validation' mongomapper (0.4.1) lib/mongomapper/save_with_validation.rb:12:in save' mongomapper (0.4.1) lib/mongomapper/embedded_document.rb:305:inupdate_attributes' app/controllers/candidates_controller.rb:63:in update' /Library/Ruby/Gems/1.8/gems/josevalim-inherited_resources-0.9.1/lib/inherited_resources/legacy/respond_to.rb:101:incall' /Library/Ruby/Gems/1.8/gems/josevalim-inherited_resources-0.9.1/lib/inherited_resources/legacy/respond_to.rb:101:in retrieve_response_from_mimes' /Library/Ruby/Gems/1.8/gems/josevalim-inherited_resources-0.9.1/lib/inherited_resources/legacy/respond_to.rb:56:inrespond_to' app/controllers/candidates_controller.rb:62:in `update'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment