【问题标题】:fields_for, formtastic, ActiveMerchant, and validation errorsfields_for、formtastic、ActiveMerchant 和验证错误
【发布时间】:2011-04-01 20:07:41
【问题描述】:

我正在尝试使用 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 上。

【问题讨论】:

  • 你还需要这个问题的答案吗?

标签: ruby-on-rails ruby formtastic activemerchant


【解决方案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,我真的不喜欢这颗宝石(但那是另一回事了)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-05
    • 1970-01-01
    • 1970-01-01
    • 2012-08-07
    相关资源
    最近更新 更多