2

我目前正在开发一个 Rails 应用程序(rails v5.1.1 和 ruby​​ v2.3.4),尝试在我的一条路线( )上使用改革/bookings/new表单对象时出现错误:

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

我正在使用virtus 模型(在其他情况下也可以正常工作):

class Booking
  include Virtus.model

  attribute :id, Integer
  attribute :client_email, String
end

这是我的表单对象:

class BookingForm < Reform::Form
  property :client_email
end

这是new我的控制器上的操作:

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

这是我的部分表格:

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

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

我认为使用 virtus 模型而不是活动记录应该没有问题,因为改革将自己推销为Form objects decoupled from your models. 我有什么不对吗?

4

1 回答 1

0

@fanta 的评论似乎有所帮助,但我的长期答案是您应该避免使用 Virtus,尤其是当您正在构建一个新项目时。Virtus 不再受到其自己团队的支持,他们转向了 dry-rb(dry-types 和 dry-validations)

如果您需要模拟模型 - 您可以使用干结构、OpenStruct 等

此外,改革现在完全支持干验证和干类型,这将是未来的方式(你 AM 将被支持到版本 4)祝你好运:-)

于 2017-07-05T20:35:31.410 回答