Search code examples
ruby-on-railsrubyreformtrailblazervirtus

undefined method `persisted?' with Reform and Virtus model


I'm currently developing an Rails app (rails v5.1.1 and ruby v2.3.4) and I'm getting an error when trying to use a reform form object at one of my routes (/bookings/new):

undefined method `persisted?' for #<Booking:0x007fbae9a98138>

I'm using a virtus model (which works fine on other contexts):

class Booking
  include Virtus.model

  attribute :id, Integer
  attribute :client_email, String
end

This is my form object:

class BookingForm < Reform::Form
  property :client_email
end

This is the new action on my controller:

def new
  @form = BookingForm.new(Booking.new)
end

This is my form partial:

<%= form_for @form do |form| %>
  <%= form.text_field :client_email %>

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>

I thought using a virtus model instead of an active record one should be no issue since reform sells itself as Form objects decoupled from your models. Did I get anything wrong?


Solution

  • It seems like the comment from @fanta helped, but I the long-term answer is that you should avoid using Virtus, especially since you are building a new project. Virtus is no longer supported by it's own team, they moved on to dry-rb ( dry-types and dry-validations)

    If you need to mock a model - you can use dry-struct, OpenStruct, etc

    Also Reform now has full support for dry-validation and dry-types, and it is going to be the way of the future ( thou AM is going to be supported till version 4) Best of luck :-)