Polymorphic associations problems #44

Closed
grimen opened this Issue Sep 6, 2009 · 10 comments

3 participants

@grimen

In certain situations with polymorphic associations @semantic_form_for@ don't generate proper @inputs@/@semantic_fields_for@ (outputs "0" wrapped in fieldset-tag), and for other not any form at all, while this still works for Rails core helpers (@form_for@ and @fields_for@).

I haven't found the source of all this evil yet, and simply got no simple failing spec - just a live project I'm working on. I switched to @form_for@ for these cases but will look into it of curiosity as soon as I'm done with some other stuff.

@justinfrench
Owner

Let me know how we can help!

@grimen

Yea, I found the source of the the "0"-output issue, but right now the implementation is a bit confusing so still trying to learn why. Here's the issue

class Account < ActiveRecord::Base
  has_one :profile, :dependent => :destroy
  accepts_nested_attributes_for :profile, :allow_destroy => false
end
class Profile < ActiveRecord::Base
  belongs_to :account
end
- semantic_form_for(@account, :url => account_path) do |f|
  - f.inputs :for => @account.profile do |p|
    = p.input :real_name   # fails: outputs '0'
    - p.input :real_name   # works
- form_for(@account, :url => account_path) do |f|
  - fields_for @account.profile do |p|
    = p.text_field :real_name   # works

Maybe anyone of you got the answer already when you see this, but I'm still a bit confused why right now. This most probably have nothing to do with the polymorphic issues I had - two separate problems.

@justinfrench

A post on the google group has suggested this is only a problem with HAML, not ERB.

@grimen

Yes, but it works perfectly for fields_for + HAML so it must be related to Formtastic somehow. I read like a year ago that 40%+ of Rails users out there use HAML (according to a Rails Envy poll if I remember correctly), so...not to hush-hush. I haven't looked into it more since I found out this - it was quite hard to debug partly becuase of the metaprogramming stuff in the bottom of formtastic.rb that confuses ruby-debug. Should look into it more, but too bored right now and got way to many balls in the air.

@justinfrench

When/if someone is in the mood, the meta-programming can easily be expanded out into something more verbose. I'd be happy to look into it all if someone can provide me with a succinct test case to work against.

@grimen

No failing specs, but I did an example app for the fields_for/has_one-issue.

http://github.com/grimen/test_app

@grimen

I got a relatively clean solution on this, but there's no documentation out there how to test HAML buffer - that seems to work with form-builders (outputting to buffer directly, not returning a string). The solution is fundamentally a one-liner, works for my test-project (not pushed).

in "field_set_and_list_wrapping"

contents = if block_given?
  if template.is_haml?
    template.capture_haml(&block)
  else
    template.capture(&block)
  end
end

Note: Neither output_buffer or haml_buffer.buffer seems to "care" about haml_concat/capture_haml in specs...so gave up on a spec after an hour. =(

@grimen

The "0"-output part is squashed by this:

http://github.com/grimen/formtastic/commit/e0a4ecddc3bc87646391350b4f4991101fcc95ab

...but I'm not sure it solves the other issue I had with the polymorphic associations. Will revisit that when I can.

@justinfrench

Awesome work! Pulled in, closing.

@EtienneDepaulis

Hi @justinfrench is it possible to have an example of how to use it on a polymorphic field ?
Cheers

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment