3

インライン エラーを使用したいので、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 からインライン エラーが発生しません。

ここに欠けている単純なものがあると思いますが、何がわからないのですか。

これはRails 3にあります。

4

1 に答える 1

1

それがあなたの望むものかどうかはわかりませんが、このコードを追加してみてください(client_side_validations gemから取得)

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

一部の初期化子(たとえば、config / initializers / form_errors.rb

Formtasticに関しては、私はこの宝石が本当に嫌いです(しかしそれは別の話です)

于 2012-09-28T16:13:12.790 に答える