Skip to content

Instantly share code, notes, and snippets.

@flsafe flsafe/gist:393116
Created May 7, 2010

Embed
What would you like to do?
#-----------------------In my form-----------------
<div id="hints">
<%= render :partial => 'hint', :collection=>@exercise.hints %>
</div>
<%= add_hint_link %>
#----------------------The partial for the hint fields---------------------------
<div class='hint'>
<% new_or_existing = hint.new_record? ? 'new' : 'existing' %>
<% prefix = "exercise[#{new_or_existing}_hint_attributes][]" %>
<% fields_for prefix, hint do |hf| %>
<%= hf.label :text, "Hint" %> <%= link_to_function 'Remove Hint', "this.up('.hint').remove()" %><br/>
<%= hf.text_area :text %><br/>
<% end %>
</div>
#-----------------------The HTML output--------------------------------
<div id="hints">
<div class='hint'>
<label for="exercise_new_hint_attributes__text">Hint</label>
<a href="#" onclick="this.up('.hint').remove(); return false;">Remove Hint</a><br/>
<textarea cols="40" id="exercise_new_hint_attributes__text" name="exercise[new_hint_attributes][][text]" rows="20></textarea><br/>
</div>
</div>
#---------------------The error--------------------------
undefined method `each' for "[{\"text\"=>\"This is hint 1\"}]":String (NoMethodError)
./app/models/exercise.rb:49:in `new_attributes_for'
./app/models/exercise.rb:21:in `new_hint_attributes='
#---------------------The code associated with the error----------
def new_hint_attributes=(attributes)
new_attributes_for(:hints, attributes)
end
def new_attributes_for(association, attributes)
associates = self.send(association)
attributes.each do |atts|
associates.build atts
end
end
#--------------------Cucumber log for params-----------------
(Notice that all the arrays are strings, but hashes are okay)
Parameters: {"exercise"=>{"title"=>"Implementing a singly linked list", "description"=>"Implement a singly linked list is a common problem", "algorithm_list"=>"numeric, primes", "data_structure_list"=>"linked lists", "problem"=>"Implementing the add function given a linked list pointer", "tutorial"=>"There are several ways to implement", "new_hint_attributes"=>"[{\"text\"=>\"This is hint 1\"}]", "minutes"=>"60", "new_unit_test_attributes"=>"[{\"unit_test_file\"=>#<ActionController::TestUploadedFile:0x3629aac @content_type=\"c\", @original_filename=\"unittest.c\", @tempfile=#<File:/var/folders/dE/dElYWBv+FpWq3NuPuovRlk+++TI/-Tmp-/unittest.c20100506-979-3t8pr5-0>>}]", "new_figure_attributes"=>"[{\"image\"=>#<ActionController::TestUploadedFile:0x3628868 @content_type=\"image/png\", @original_filename=\"test.png\", @tempfile=#<File:/var/folders/dE/dElYWBv+FpWq3NuPuovRlk+++TI/-Tmp-/test.png20100506-979-1hq2uau-0>>}]", "exercise_set_id"=>"", "new_exercise_set_attributes"=>{"title"=>"Linked List Basics", "description"=>"Implement linked lists", "algorithm_list"=>"Traverse Linked List", "data_structure_list"=>"Linked Lists"}}, "commit"=>"Save changes", "controller"=>"exercises", "action"=>"create"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.