Skip to content
This repository
Browse code

[merb-helpers] Select the correct field in select tags

  • Loading branch information...
commit e2e3866efa39e5a307fdd406005e0012c02e3c90 1 parent fcf8ced
Tymon Tobolski authored October 26, 2009 snusnu committed October 27, 2009
2  merb-helpers/lib/merb-helpers/form/builder.rb
@@ -164,7 +164,7 @@ def update_bound_check_box(method, attrs)
164 164
     def update_bound_select(method, attrs)
165 165
       attrs[:value_method] ||= method
166 166
       attrs[:text_method] ||= attrs[:value_method] || :to_s
167  
-      attrs[:selected] ||= control_value(attrs[:value_method])
  167
+      attrs[:selected] ||= control_value(method)
168 168
     end
169 169
 
170 170
     def update_unbound_controls(attrs, type)
3  merb-helpers/spec/fixture/app/views/bound_select_specs/selected.html.erb
... ...
@@ -0,0 +1,3 @@
  1
+<%= form_for @obj do %>
  2
+<%= select( :bar, :collection => @collection, :text_method => :bar, :value_method => :foo ) %>
  3
+<% end =%>
13  merb-helpers/spec/merb_helpers_form_spec.rb
@@ -984,6 +984,19 @@
984 984
     form.should have_selector("label[for=fake_model_foo]:contains('LABEL')")
985 985
   end
986 986
 
  987
+  it "should render a select tag with correct field selected" do
  988
+    a = FakeModel3.new; a.bar = "A"; a.foo = 4
  989
+    b = FakeModel3.new; b.bar = "B"; b.foo = 2
  990
+    c = FakeModel3.new; c.bar = "C"; c.foo = 7
  991
+
  992
+    @c.instance_variable_set(:@collection, [a,b,c])
  993
+
  994
+    r = @c.render :selected
  995
+    r.should_not have_selector("select[id=fake_model_bar] option[selected]:contains('A')")
  996
+    r.should_not have_selector("select[id=fake_model_bar] option[selected]:contains('B')")
  997
+    r.should have_selector("select[id=fake_model_bar] option[selected]:contains('C')")
  998
+  end
  999
+
987 1000
   # Not sure how this makes any sense
988 1001
   # ---------------------------------
989 1002
   #

0 notes on commit e2e3866

Please sign in to comment.
Something went wrong with that request. Please try again.