2010-08-20 51 views
3

我試圖使用Formtastic來創建付款表單,因爲我想使用內聯錯誤。我正在使用ActiveMerchant來處理結算。我有以下形式:fields_for,formtastic,ActiveMerchant和驗證錯誤

<%= semantic_form_for @payment do %> 
    <%= form.inputs do %> 
    <%= form.input :email, :label => "Email Address" %> 

    <%= form.semantic_fields_for :credit_card_attributes do |cc| %> 
     <%= cc.input :number, :label => "Credit Card Number" %> 
     <%= cc.input :first_name, :label => "First Name" %> 
     <%= cc.input :last_name, :label => "Last Name" %> 
     <%= cc.input :month, :label => "Expiration Month" %> 
     <%= cc.input :year, :label => "Expiration Year" %> 
     <%= cc.input :verification_value, :label => "Verification Code" %> 
    <% end %> 
    <% end %> 
<% end %> 

而且這是在我的Payment模型:

class Payment < ActiveRecord::Base 
    validates_associated :credit_card, :on => :create 

    def credit_card_attributes=(attrs) 
    @credit_card = ActiveMerchant::Billing::CreditCard.new(attrs) 
    end 

    def credit_card 
    @credit_card 
    end 
end 

當我提交無效信用卡,它的數字出來,它是無效的,但我不明白來自formtastic的任何內聯錯誤。

我覺得這裏可能有些簡單的東西,我在這裏錯過了,我不知道是什麼。

這是on Rails的3

+0

你ST生病需要在這方面的答案? – peresleguine 2012-09-26 10:26:13

回答

1

我不知道這是你想要的,但你可以添加以下代碼(以從client_side_validations寶石)

ActionView::Base.field_error_proc = Proc.new do |html_tag, instance| 
    unless html_tag =~ /^<label/ 
    %{<div class="field_with_errors">#{html_tag}<label for="#{instance.send(:tag_id)}" class="message">#{instance.error_message.first}</label></div>}.html_safe 
    else 
    %{<div class="field_with_errors">#{html_tag}</div>}.html_safe 
    end 
end 

在一些初始化(例如配置/initializers/form_errors.rb

至於Formtastic,我真的不喜歡這種寶石(但這是另一回事)