-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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.
This page was created by the OSS community and might be outdated or incomplete. Feel free to improve or update this content according to the latest versions of SimpleForm and Rails to help the next developer who visits this wiki after you.
Keep in mind to maintain the guides as simple as possible and to avoid additional dependencies that might be specific to your application or workflow (such as Haml, RSpec, Guard and similars).