2012-04-19 31 views
1

我有一個信用卡類,我想對各種屬性使用simple_form f.input方法用於驗證等,但我收到有關model_name的錯誤。有沒有簡單的方法來做到這一點?如何將simple_form與普通的Ruby類一起使用?

= 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' 

回答

4

你需要符合API加載ActiveModel ...

有效:

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 

一些背景資料:

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

謝謝!我剛剛發現Railscast情節,並得到它的工作。但是如何持續?一直是錯誤的效果?我得看看那個。 – 2012-04-19 15:09:52

+0

@EricM。它似乎會影響表單中使用的HTTP動詞:'false'產生'POST','true'產生'PATCH'。至少我至今還沒有發現任何其他影響。 – 2015-05-26 15:53:28

相關問題