Skip to content

Use with ActiveModel compliant models

Lucas Mazza edited this page Jan 27, 2013 · 1 revision

Use with ActiveModel compliant models

SimpleForm can also be used with any ActiveModel-compliant objects (such as created using ActiveAttr).

The only thing you have to keep in mind is to define <attribute>_attributes= setter for the nested models. This is only required for non-ActiveRecord models where accepts_nested_attributes_for is used instead. See the Rails' fields_for for more information.

It is also possible to "mix and match" different types of models (such as ActiveRecord and poor ActiveModel ones).

For example:

class ReservationFee < ActiveRecord::Base
  attr_accessor :credit_card

  # This is required by SimpleForm and Rails for non-ActiveRecord nested attributes
  def credit_card_attributes=(attributes)
    @credit_card = CreditCard.new(attributes)
  end
end

class CreditCard
  include ActiveAttr::Model
  attribute :number
  attribute :expiry
  attribute :cvn
  attribute :name
end

Provided that you have it, SimpleForm can be used to generate Nested Models.