3

I am following along with railscast #288 on stripe but I am having some difficulty with setting the stripe hidden token. I am using the most recent version of devise with simple form integration.

Here is my Registrations Controller:

class RegistrationsController < Devise::RegistrationsController

  def create
    build_resource 
    if resource.save_with_payment
      if resource.active_for_authentication?
        set_flash_message :notice, :signed_up if is_navigational_format?
        sign_in(resource_name, resource)
        respond_with resource, :location => after_sign_up_path_for(resource)
      else
        set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
        expire_session_data_after_sign_in!
        respond_with resource, :location => after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords resource
      respond_with resource
    end
  end

My form for registering a new user:

<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => {:class => 'well'} ) do |f| %>
  <%= f.error_notification %>
  <%= f.error_messages %>
  <h3 class="center">Create your credentials for logging in:</h3><hr>
  <div class="inputs">
    <%= f.input :email, :required => true  %>
    <%= f.input :password, :required => true %>
    <%= f.input :password_confirmation, :required => true %>
  <h3 class="center">Are you a Shipper or a Carrier?</h3><hr>
    <%= f.input :role, :collection => User::ROLES, :as => :radio_buttons %>
    <%= f.input :stripe_card_token, :as => :hidden %>
  </div>
  <h3 class="center">Your Credit Card Information:</h3><hr>
  <fieldset>
    <div class="field">
      <%= label_tag :card_number, "Credit Card Number" %>
      <div class="input-prepend inline-block">
      <span class="add-on"><strong>#</strong></span>
      <%= text_field_tag :card_number, nil, name: nil %>
    </div>
    <div class="field">
      <%= label_tag :card_code, "Security Code on Back of Card (CVV)" %>
      <div class="input-prepend inline-block">
      <span class="add-on icon-credit-card"></span>
      <%= text_field_tag :card_code, nil, name: nil, :size => 4  %>
    </div>
    <div class="field">
      <%= label_tag :card_month, "Card Expiration" %>
      <%= select_month nil, { add_month_numbers: true }, { name: nil, id: "card_month" } %>
      <%= select_year nil, { start_year: Date.today.year, end_year: Date.today.year+15 },
  { name: nil, id: "card_year" } %>
    </div>
    <div id="stripe_error">
      <noscript>JavaScript is not enabled and is required for this form. First enable it in your web browser settings.</noscript>
    </div>
  </fieldset>
  <hr>
  <%= f.input :terms, :as => :boolean, label: "* I have read and agreed to the terms of service" %>
  <div class="actions">
    <%= f.button :submit, "Sign up", class: "btn-large icon-thumbs-up" %>
  </div>
<% end %>

I have a user model with the following attributes:

attr_accessible :email, :password, :password_confirmation, :remember_me, :role, :terms, :stripe_token, :stripe_card_token

And Finally the coffeescript to get this all to work correctly:

jQuery ->
Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
subscription.setupForm()

subscription = 
  setupForm: ->
    $('#new_user').submit ->
        $('input[type=submit]').attr('disabled', true)
        if $('#card_number').length
            subsciption.processCard()
            false
        else
            true

processCard: ->
    card =
        number: $('#card_number').val()
        cvc: $('#card_code').val()
        expMonth: $('#card_month').val()
        expYear: $('#card_year').val()
    Stripe.createToken(card, subscription.handleStripeResponse)

handleStripeResponse: (status, response) ->
    if status == 200
        $('#user_stripe_card_token').val(response.id)
        $('#new_user')[0].submit()
    else 
        $('#stripe_error').text(response.error.message)
        $('input[type=submit]').attr('disabled', false)

When I go to create a new user my action controller is giving me the following error message:

undefined method `stripe_card_token' for #<User:0x00000103fe7b90>

However I don't think this should be possible since I have defined this method in my attr_accessible call in my user.rb model. Any explanation would be really helpful, as I think i am 90% of the way to being done with setting this up.

0

1 Answer 1

5

Unless stripe_card_token is a field in your Users table, you'll want to make it an attr_accessor (as well as attr_accessible).

1
  • That's interesting, I thought that attr_accessible would be also doing attr_accessor. Would you know why it's not? thanks
    – Don Giulio
    Mar 25, 2014 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.