Fields_for, formtastic, ActiveMerchant and validation errors

I am trying to use Formtastic to create a payment form, since I would like to use inline errors. I use ActiveMerchant to handle billing. I have the following form:

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

And here is what is in my model 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

When I send an invalid credit card, it indicates that it is invalid, but I do not receive any built-in errors from formtastic.

I suppose that maybe something simple is missing here, I'm just not sure what.

This is on Rails 3.

+3
source share
1 answer

, , ( 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

(, config/initializers/form_errors.rb)

Formtastic, ( )

+1

Source: https://habr.com/ru/post/1760803/


All Articles