Eric M.
Eric M.

Reputation: 5529

How do I use simple_form with a plain Ruby class?

I have a credit card class and I'd like to use the simple_form f.input method on the various attributes for use with validations, etc, but I'm getting errors about model_name. Is there a simple way to do this?

= simple_form_for @credit_card, url: join_network_path do |f|
    .payment-errors
  = f.input :card_number
  = f.input :cvc
  = f.input :expiry_month
  = f.input :expiry_year
  = f.submit 'Join Network'

Upvotes: 1

Views: 1621

Answers (1)

Jesse Wolgamott
Jesse Wolgamott

Reputation: 40277

You'll need to conform to the ActiveModel API...

Effectively:

class CreditCard
  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming

  def initialize(attributes = {})
    attributes.each do |name, value|
      send("#{name}=", value)
    end
  end

  def persisted?
    false
  end
end

Some background:

  1. http://yehudakatz.com/2010/01/10/activemodel-make-any-ruby-object-feel-like-activerecord/
  2. http://railscasts.com/episodes/219-active-model

Upvotes: 4

Related Questions