Nick McNeany

Joined

9,120 Experience
71 Lessons Completed
1 Question Solved

Activity

Posted in Layout for Devise with scoped route

Hey Everyone,

I'm trying to use a custom layout for my devise/registrations#edit view. I'm using a scoped route.

devise_scope :member do
        get "/members/:id/settings" => "devise/registrations#edit", as: "edit_member_registration"
end

I call the layout in my registrations controller layout "member_area", only: :edit, but it's not showing.

I have a feeling it has to do with the scoped route. Also, if I take off the only: :edit the layout shows up on the other registration pages as expected.

Anyone have any thoughts on this?

Thanks for any help!

Nick

Posted in Spree commerce vs custom solution from scratch

Awesome, Thanks for the update and resources @Damian!

Another cool idea, would be the ability to update the map with locations near a user based on their location/ip address, either automatically or with a manual reload. I know this is difficult to do in development, but would love to see this.

Awesome episode! Really enjoying the maps and geolocation episodes!

Thanks, Chris!

Posted in Stripe Subscriptions: Duplicate Customers

Haha, I know, right!!!

Actually, in the Stripe Elements screencast you use a form_tag because you're using Rails 5.1.0. You attempt to use the form_with, realize you can't and then move on with the form_tag.

Posted in Stripe Subscriptions: Duplicate Customers

Ummm... No, no I did not have local: true on the form_with.

Man!!! How did I miss that!!!

By default form_with attaches the data-remote attribute submitting the form via an XMLHTTPRequest in the background if an Unobtrusive JavaScript driver, like rails-ujs, is used. See the :local option for more. source: http://edgeapi.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-formwith

Thanks, Chris! You really are the man!! Appreciate all your help!!!

Posted in Stripe Subscriptions: Duplicate Customers

Hey Everyone,

Just wanted to give a quick update. So, I was still getting some really weird behavior like, duplicate customers and resubscribing was creating 4 new subscriptions!

I was using a form_with tag for the payment forms. I changed that to a form_tag and now everything is working as expected.

Not sure why the form_with was causing all this disaray, maybe I was using it wrong... I don't know. I do know changing it to a form_tag fixed it.

Has anyone dealt with this before or have any idea what was going on with the form_with helper?

Thanks!

Posted in Stripe Subscriptions: Duplicate Customers

Thanks, Chris! You the man!

Posted in Stripe Subscriptions: Duplicate Customers

Thanks Chris,

I was getting some erros from rails-ujs, so I swapped if for jquery-ujs since I'm using jquery-rails and that seems to have fixed the dupicate customer problem.

I'm still getting some Stripe releated errors that I'm looking into now. I may endup opening another thread with more detail, but in short I keep getting an error saying (paraphrased) :

#card-element can't be found and to make sure it's on the page before calling .mount

#card-element is definitely on my form view, so I'm assuming it has to do with the order of my js being loaded or turbolinks or both.

Has anyone experienced this? I'm using Rails 5.1.x.

Thanks!

Posted in NameError: uninitialized constant Stripe

Did you run bundle install after adding the Stripe gem?

Also, did you try stopping and restarting your rails console? Your changes may not have taken affect if your console was running when you made them.

Posted in Stripe Subscriptions: Duplicate Customers

Hey Eeveryone,

I'm creating a subscription site using Stripe v3. For the most part everything is working fine, but I noticed in my Stripe Customer dashboard there are duplicate customers (for all customers). The first customer does not have a card associated with it, but the duplicate customer does.

I noticed in my logs that it is performing an update to the member two times right in a row, but I can't figure out what's causing this to happen. Pasted logs at the very bottom.

Here's all my stuff

I know it's a lot to look through, but I really appreciate any help!

Member Model

def stripe_customer
        if stripe_id?
                Stripe::Customer.retrieve(stripe_id)
        else
                stripe_customer = Stripe::Customer.create(email: email)
                update(stripe_id: stripe_customer.id)
                stripe_customer
        end
end

Memberships Controller

before_action :authenticate_member!

def new
end

def create
        customer = current_member.stripe_customer

    begin
            subscription = customer.subscriptions.create(
                    source: params[:stripeToken],
                    plan:   params[:plan]
            )

            current_member.assign_attributes(stripe_subscription_id: subscription.id, expires_at: nil)
            current_member.assign_attributes(
                    card_brand:     params[:card_brand],
                    card_last4:     params[:card_last4],
                    card_exp_month: params[:card_exp_month],
                    card_exp_year:  params[:card_exp_year]
            ) if params[:card_last4]
            current_member.save

            flash.notice = "BooYah!!! Thanks for signing up!"
            redirect_to edit_member_registration_path(current_member)
    rescue Stripe::CardError => e
            flash.alert = e.message
            render action: :new
    end
end

def show
end

Stripe Elements

document.addEventListener("turbolinks:load", function() {
    var public_key = document.querySelector("meta[name='stripe-public-key']").content;
    var stripe = Stripe(public_key);
    var elements = stripe.elements();

    // Custom styling can be passed to options when creating an Element.
    var style = {
        base: {
            // Add your base input styles here. For example:
            fontSize: '16px',
            lineHeight: '24px'
        }
    };

    // Create an instance of the card Element
    var card = elements.create('card', {style: style});

    // Add an instance of the card Element into the `card-element` <div>
    card.mount('#card-element');

    card.addEventListener('change', function(event) {
        var displayError = document.getElementById('card-errors');
            if (event.error) {
            displayError.textContent = event.error.message;
        } else {
            displayError.textContent = '';
        }
    });

    // Create a token or display an error when the form is submitted.
    var form = document.getElementById('payment-form');
        form.addEventListener('submit', function(event) {
        event.preventDefault();

        stripe.createToken(card).then(function(result) {
            if (result.error) {
                // Inform the user if there was an error
                var errorElement = document.getElementById('card-errors');
                errorElement.textContent = result.error.message;
            } else {
                // Send the token to your server
                stripeTokenHandler(result.token);
            }
        });
    });
});

function stripeTokenHandler(token) {
    // Insert the token ID into the form so it gets submitted to the server
    var form = document.getElementById('payment-form');
    var hiddenInput = document.createElement('input');
    hiddenInput.setAttribute('type', 'hidden');
    hiddenInput.setAttribute('name', 'stripeToken');
    hiddenInput.setAttribute('value', token.id);
    form.appendChild(hiddenInput);

    ["brand", "exp_month", "exp_year", "last4"].forEach(function(field) {
        addFieldToForm(form, token, field);
    });

    // Submit the form
    form.submit();
}

function addFieldToForm(form, token, field) {
    var hiddenInput = document.createElement('input');
    hiddenInput.setAttribute('type', 'hidden');
    hiddenInput.setAttribute('name', "card_" + field);
    hiddenInput.setAttribute('value', token.card[field]);
    form.appendChild(hiddenInput);
}

Logs

Started POST "/membership" for 127.0.0.1 at 2017-09-03 14:04:15 -0500
Processing by MembershipsController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"tF7nwOkJPALmvzrH5UVC4TgxolKBYH8pRQYkVpK8tSFG8ADR4dQnJmWHYBYSuQq2pI8OdvtXvqgIIoRQ3/fIXA==", "plan"=>"annual"}

  Member Load (0.6ms)  SELECT  "members".* FROM "members" WHERE "members"."id" = $1 ORDER BY "members"."id" ASC LIMIT $2  [["id", 15], ["LIMIT", 1]]
Started POST "/membership" for 127.0.0.1 at 2017-09-03 14:04:16 -0500
Processing by MembershipsController#create as HTML

  Parameters: {"utf8"=>"✓", "authenticity_token"=>"tF7nwOkJPALmvzrH5UVC4TgxolKBYH8pRQYkVpK8tSFG8ADR4dQnJmWHYBYSuQq2pI8OdvtXvqgIIoRQ3/fIXA==", "plan"=>"annual", "stripeToken"=>"tok_1Ay3OGFjHiphw0iAWDy0deos", "card_brand"=>"Visa", "card_exp_month"=>"12", "card_exp_year"=>"2023", "card_last4"=>"0002"}
  Member Load (0.6ms)  SELECT  "members".* FROM "members" WHERE "members"."id" = $1 ORDER BY "members"."id" ASC LIMIT $2  [["id", 15], ["LIMIT", 1]]

    |==This where I think the duplication is coming from. But I can't figure out why or what's causing it
    ==================================================================================
   (0.2ms)  BEGIN
  SQL (0.7ms)  UPDATE "members" SET "stripe_id" = $1, "updated_at" = $2 WHERE "members"."id" = $3  [["stripe_id", "cus_BKipFeUFJtHX6t"], ["updated_at", "2017-09-03 19:04:17.125942"], ["id", 15]]
   (6.6ms)  COMMIT
   (0.2ms)  BEGIN
  SQL (0.6ms)  UPDATE "members" SET "stripe_id" = $1, "updated_at" = $2 WHERE "members"."id" = $3  [["stripe_id", "cus_BKipmnHyvuuvVH"], ["updated_at", "2017-09-03 19:04:17.252095"], ["id", 15]]
   (0.5ms)  COMMIT
        ==================================================================================

Completed 500 Internal Server Error in 1995ms (ActiveRecord: 2.0ms)

Stripe::InvalidRequestError (This customer has no attached payment source):

app/controllers/concerns/memberships_controller.rb:11:in `create'
  Rendering memberships/new.html.erb within layouts/application
  Rendered memberships/new.html.erb within layouts/application (0.9ms)
  Rendered shared/_flash.html.erb (0.8ms)
Completed 200 OK in 1625ms (Views: 59.6ms | ActiveRecord: 8.2ms)

Also, I'm not sure why I'm seeing these two lines in my log:

Completed 500 Internal Server Error in 1995ms (ActiveRecord: 2.0ms)

Stripe::InvalidRequestError (This customer has no attached payment source):

Again, I know this is a lot to go through, but I really appreciate any help anyone can offer!

Please let me know if anyone needs any additional info!

Thanks!

Nick

Posted in Do I need rails-ujs and jquery_ujs?

My lazyness has been exposed! I didn't even bother to search the videos, haha!

Thanks for the help Chris! You're the man!

Posted in Do I need rails-ujs and jquery_ujs?

I'm bulding a rails 5.1.x app so no jQuery, but I'm usining Bootstrap so I added it in.

I was adding in //= require jquery and //= require jquery_ujs, then noticed that rails 5.1 now ships with //= require rails-ujs. My initial reaction is rails-ujs replaces the need for jquery_ujs so I don't need it.

My question is, will I run into any problems with jQuery if I do not include //= require jquery_ujs ?

Thanks for the help!

Nick

Congrats Chris! You've created and awesome community and your videos have always been top notch!

Great video! I'm really interested in seeing more Geolocation/Geocoder videos!

Posted in Tracking Metrics with Ahoy and Blazer Discussion

Awesome episode! Thanks Chris!

Posted in Feature Flags with Rollout Discussion

Very cool! Thanks, Chris!

Posted in Global Autocomplete Search Discussion

Awesome episode Chris! Thanks!

Posted in Hatch: Deploying my first App

No, I definitely connected all my accounts way before the server was provisioned. I added all my accounts right when I signed up, then a couple weeks later created my first server.

Let me know if you have anymore questions or if there's anything else I can do to help.

Posted in Hatch: Deploying my first App

Chris! you are the man, it's there! Thanks for the awesome, quick support!

Haha, I did see the email notifacation. Your secret identity is safe with me ;)

Posted in Hatch: Deploying my first App

Hey Chris,

Thanks for the quick response!

  • Added the SSH Key to the already created sever. Problem solved!
  • I dont see an SSH Key from Hatch anywhere in my BitBucket settings. Is there a way to do this manually?

Let me know if you need more info.

Thanks again for your help!

Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

© 2024 GoRails, LLC. All rights reserved.