Permalink
Browse files

Spec coverage to confirm that a work-around exists for GH-398.

Rails will automatically render a hidden id input for each nested object unless a hidden id field has already been rendered. The hidden input Rails renders is invalid HTML in the context of ol and li tags. The work-around is to render your own f.input :id, :as => :hidden, which renders the correct mark-up.

TODO: Still need to try to render our own hidden field in the block WITHOUT requiring the author to implement this work-around.
  • Loading branch information...
1 parent 6653c2d commit 03ef4a1a55a818b0480dec28c98134acc5364335 @justinfrench committed Oct 17, 2010
Showing with 28 additions and 0 deletions.
  1. +27 −0 spec/semantic_fields_for_spec.rb
  2. +1 −0 spec/spec_helper.rb
@@ -40,6 +40,33 @@
output_buffer.should_not =~ /id="post\[author\]_1_login_input"/
# <=> output_buffer.should_not have_tag('form fieldset.inputs #post[author]_1_login_input')
end
+
+ context "when I rendered my own hidden id input" do
+
+ before do
+ output_buffer.replace ''
+
+ @fred.posts.size.should == 1
+ @fred.posts.first.stub!(:persisted?).and_return(true)
+ @fred.stub!(:posts_attributes=)
+
+ form = semantic_form_for(@fred) do |builder|
+ concat(builder.semantic_fields_for(:posts) do |nested_builder|
+ concat(nested_builder.input(:id, :as => :hidden))
+ concat(nested_builder.input(:title))
+ end)
+ end
+ output_buffer.concat(form) if Formtastic::Util.rails3?
+ end
+
+ it "should only render one hidden input (my one)" do
+ output_buffer.should have_tag 'input#author_posts_attributes_0_id', :count => 1
+ end
+
+ it "should render the hidden input inside an li.hidden" do
+ output_buffer.should have_tag 'li.hidden input#author_posts_attributes_0_id'
+ end
+ end
end
View
@@ -192,6 +192,7 @@ def new_author_path; "/authors/new"; end
@freds_post.stub!(:class).and_return(::Post)
@freds_post.stub!(:to_label).and_return('Fred Smith')
@freds_post.stub!(:id).and_return(19)
+ @freds_post.stub!(:title).and_return("Hello World")
@freds_post.stub!(:author).and_return(@fred)
@freds_post.stub!(:author_id).and_return(@fred.id)
@freds_post.stub!(:authors).and_return([@fred])

0 comments on commit 03ef4a1

Please sign in to comment.