When using Braintree and their Javascript integration library, you have to append a data-encrypted-name to the input field to allow the Braintree JS client to select, encrypt, and replace the value.

When working with Rails, the input name generally includes the form name and the attribute name such as: add_credit_card[credit_card_number]... so you might be doing:

f.input :credit_card_number, as: :string, data: { encrypted_name: 'add_credit_card[credit_card_number]' }  

but if you ever change the attribute name or the name of the form, or move it somewhere else, the values may change and you have to remember to update the data-encrypted-name manually.

If you're using SimpleForm 2+, I built a custom input type that will automatically append the data-encrypted-name for you... no more manually adding or editing it.

Simply add:

# app/inputs/braintree_encrypted_input.rb

class BraintreeEncryptedInput < SimpleForm::Inputs::StringInput  
  def input(wrapper_options = nil)
    input_html_options[:'data-encrypted-name'] = "#{object_name}[#{attribute_name}]"
    super(wrapper_options)
  end
end  

to your project and use it in your forms:

f.input :credit_card_number, as: :braintree_encrypted  
Written By
Share