unabridged/motion

Motion component is unable to have a handle to a form object

jcoyne opened this issue · 7 comments

I'm seeing this error:

Some state prevented `ChildComponent` from being serialized into a string. Motion components must be serializable using `Marshal.dump`. Many types of objects are not serializable including procs, references to anonymous classes, and more. See the documentation for `Marshal.dump` for more information.

The specific error from `Marshal.dump` was: can't dump anonymous class #<Class:0x00007f8761603a70>

Hint: Ensure that any exotic state variables in `ChildComponent` are removed or replaced.

It would be nice if we could use rails form objects in motion components.

parent_component.html.erb

  <%= form_with model: @work do |f| %>
    Parent <%= @work.title %>
    <%= render ChildComponent.new(form: f, update: bind(:update_model)) %>
  <% end %>

parent_component.rb

class ParentComponent < ViewComponent::Base
  include Motion::Component

  attr_reader :work

  def initialize(work:)
    @work = work
  end

  def update_model(opts)
    @work.attributes = opts
    true
  end
end

child_component.html.erb

       <input id="title" data-motion="add" class="form-control">

child_component.rb

class ChildComponent < ViewComponent::Base
  include Motion::Component

  def initialize(form:, update:)
    @form = form
    @update = update
  end

  map_motion :add

  attr_reader :form

  def add(event)
    element = event.current_target
    @update.call({title: element.value})
  end
end

If you can hop on master, this was recently merged: #47

@caifara Nice! I'll give that a try.

@caifara master does work, however I was unable to call current_target on the event:

[TitleComponent:34660] An error occurred while processing add:
	NoMethodError: undefined method `current_target' for #<Motion::Event:0x00007fa0d8738b80>

however, I was able to switch to using target just fine.

@jcoyne current_target was removed in #43 because I don't think it is what we ever wanted. On master (and in the next release), Motion::Event#element will always be the element to which data-motion was added.

I don't want to do a release until I track down what is causing the intermittent CI failures (especially since those are happening much more often now on master).

@alecdotninja current_target is still referred to in the README. Should that be removed?

@jcoyne Yes, that is a good idea. I really need to get better about documentation. 🙈

#47 was released in 0.4.3 and the README has been updated 🎉