lyang / braintree-rails

MIT License
65 stars 22 forks source link

Form is being generated differently than example app. #10

Closed kbighorse closed 10 years ago

kbighorse commented 11 years ago

Working from the braintree-rails-example code, copied this from TransactionsController:

class TransactionsController < ApplicationController
  before_filter :find_transactions

  def new
    @transaction = @transactions.build(amount: (1..25).to_a.sample)
  end

  def create
    @transaction = @transactions.build(params[:transaction])
  end

  private

  def find_transactions
    @transactions = BraintreeRails::Transactions.new(nil)
  end

end

Getting this error in #create: TypeError (can't convert nil into Hash):

Sure enough:

{"utf8"=>"✓",
 "authenticity_token"=>"c4J1kjuAWYKcUSXkUH9GBnkI3dPyKbcWKQT9h/sW2og=",
 "braintree_rails_transaction"=>{"amount"=>"10",
 "credit_card"=>{"number"=>"4111111111111111",
...

(also curious is that the CC data aren't being encrypted)

My form is exactly the same as https://github.com/lyang/braintree-rails-example/blob/master/app/views/transactions/new.html.haml and https://github.com/lyang/braintree-rails-example/blob/master/app/views/transactions/_form.html.haml, so I won't copy them here.

I will post the relevant generated HTML: https://gist.github.com/kbighorse/5497038.

I'll just use params[:braintree_rails_transaction] in the meantime, but pretty sure something went wrong between the controller and the view.

lyang commented 11 years ago

Hi, @kbighorse

I think the missing piece for the encryption is the patch here:

https://github.com/lyang/braintree-rails-example/blob/master/lib/rails_ext/tag_helper_ext.rb

As to the params hash, I don't know the exact cause of it.

Seems the live example at http://braintree-rails-example.herokuapp.com/transactions/new generates the correct name. Maybe it's ruby version?

The code that supposed to remove the 'braintree_rails' part from the 'braintree_rails_transaction' is at https://github.com/lyang/braintree-rails/blob/master/lib/braintree_rails/model.rb#L2

The code above should take care of the namespace, the rest of the naming should be handled by https://github.com/lyang/braintree-rails/blob/master/lib/braintree_rails/model.rb#L11

kbighorse commented 11 years ago

Hmm, adding tag_helper_ext.rb didn't help.

kbighorse commented 11 years ago

The problem is that the same HAML:

# /app/views/transactions/_form.haml
= cc.input :number, :input_html => { 'data-encrypted-name' => true }

produces different HTML:

My app:

<input class="string required"
data-encrypted-name="true"
id="braintree_rails_transaction_credit_card_number"
name="braintree_rails_transaction[credit_card][number]"
required="required"
size="50"
type="text" />

Example app:

<input class="string required"
data-encrypted-name="transaction[credit_card][number]"
id="transaction_credit_card_number"
size="50"
type="text" />

It's not replacing the name attribute with data-encrypted-name, but rather is adding data-encrypted-name as true as well as name.

There's too much magic in the example app that is actually necessary for it to work I think.

lyang commented 11 years ago

I think the problem is the tag_helper is not loaded.

I agree the helper isnt very obvious at first. A more explicit way of implementing it is probably better.

Thanks for the feedback.

Sent from my iPhone

On May 1, 2013, at 18:24, Kimball Bighorse notifications@github.com wrote:

The problem is that the same HAML:

/app/views/transactions/_form.haml

= cc.input :number, :input_html => { 'data-encrypted-name' => true } produces different HTML:

My app:

<input class="string required" data-encrypted-name="true" id="braintree_rails_transaction_credit_card_number" name="braintree_rails_transaction[credit_card][number]" required="required" size="50" type="text" /> Example app:

<input class="string required" data-encrypted-name="transaction[credit_card][number]" id="transaction_credit_card_number" size="50" type="text" /> It's not replacing the name attribute with data-encrypted-name, but rather is adding data-encrypted-name as true as well as name.

There's too much magic in the example app that is actually necessary for it to work I think.

— Reply to this email directly or view it on GitHub.

kbighorse commented 11 years ago

Is there a way to make sure it loads?

lyang commented 11 years ago

I explicitly required it in the initializers, like https://github.com/lyang/braintree-rails-example/blob/master/config/initializers/braintree.rb#L1

kbighorse commented 11 years ago

awesome, that fixed that. I'll leave this open in case someone figures out the model name thing.

kitwalker12 commented 11 years ago

in formtastic I ensured that by adding :name=>nil to the fields.

<%= f.input :number, :label  => 'Card Number', input_html: { "data-encrypted-name" => "credit_card[number]", :name => nil } %>

generates

<div class="controls">
    <input data-encrypted-name="credit_card[number]" id="credit_card_number" type="text">
</div>