Skip to content

undefined method persisted?' (and to_key) for #<Hash> when using Composition and form_for` #63

@TylerRick

Description

@TylerRick

Without Composition, persisted? works fine, but with Composition it gives this error apparently because it delegates to its model which is a Hash of multiple models.

Steps to reproduce

class MyForm < Reform::Form
  include Composition
  model :a

  property :prop_1, on: :a
  property :prop_2, on: :b
end

form = MyForm.new(a: OpenStruct.new, b: OpenStruct.new)
form.persisted?  # undefined method `persisted?' for {:a=>#<OpenStruct>, :b=>#<OpenStruct>}:Hash

Using it in a form with form_for @form also generates the error, since form_for calls persisted?.

Workaround

An easy workaround is to simply add this to form class:

  def persisted?
    false
  end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions