Chris Oliver

Joined

292,890 Experience
93 Lessons Completed
295 Questions Solved

Activity

Posted in Render partial via AJAX without JQuery

Just because Rails isn't using it, doesn't mean you should think it's going out of style. It's still used by like half the internet lol. Feel free to use jQuery especially because there are so many libraries that use it. If you choose not to, you can't use any of them and have to find alternatives or build things from scratch. 

With methods like insertAdjacentHTML becoming more cross browser compatible, jQuery just isn't as needed anymore. It was mostly designed as a compatibility layer to make things easier across browsers. That's why Rails said well, it's probably good enough to drop the dependency and simplify our framework a little. Less dependencies is usually good.

You also have Babel which lets you write modern Javascript and it compiles down to backwards compatible JS. This is kind of a different approach to what jQuery does and is what everyone uses for building Vue, React, Ember, etc apps. The nice part here is you get the compatibility but get to use new JS features (the language still sucks though 😜). 

I am leaning towards this more often and actually find using Stimulus JS or Vue to be far cleaner than my jQuery code. Stimulus doesn't have any rendering, so you'd still have to do the above, but it replaces the event handling that you typically do with jQuery. It's so nice. If you want template rendering client side, then Vue.js is awesome and is very similar. I've been leaning towards both of these over jQuery lately, but still use jQuery quite often (things like Bootstrap still require it). I mix and match right now basically.

Posted in Render partial via AJAX without JQuery

I do my best. :) 

Btw, you might want to check with that method's browser compatibility if you need IE support. I'm not sure how modern any JS methods are these days because of babel...

Posted in Render partial via AJAX without JQuery

Hey Ryan,

I believe that what's happening is that the browser doesn't know it's HTML and so it escapes the string for safety.

Here's a post on SO that shows how to insert HTML: https://stackoverflow.com/a/9851500/277994

var d1 = document.getElementById('one');

d1.insertAdjacentHTML('beforeend', '<div id="two">two</div>');

This insertAdjactentHTML method is super handy because it lets you choose where around that element the new content should go. You can see the options for it here: https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML

I think that should do what you need!

Posted in Rails Application Templates Discussion

Make sure you're using Rails 5.2 when you use the template. We're only going to support that version since it's almost released and we can take advantage of things like ActiveStorage.

Posted in How would you set up these plans in Stripe?

Hey Daniel!

You've hit the point where Stripe subscriptions break down I see. :) Unfortunately they don't offer anything too complex like this and it's a bit disappointing.

I like your idea on adjusting the invoices. That would be an easy adjustment each month and wouldn't be too hard.

A slight alternative to that would be to build your own custom billing system. Basically a cron job that runs nightly and processes payments for users who subscribed that day of the month (or just run payments every 1st of the month or something). You can then do the calculations and create a Charge object each time without dealing with Stripe Subscription objects at all.

You'd have infinite more flexibility this way, but a bit more work on your shoulders to manage. Kind of depends on how pricing might change in the future, but I'd probably consider doing it from scratch at this point, although your base subscription + adjustments model does a pretty darn good job of balancing Stripe Subscriptions without having to resort to a completely custom system.

Posted in Braintree Error In Production Mode

If your new.html.erb template uses the @plans variable, you'll have to also set it before you render it in create.

I would print out the error in the logs so you can view them and see what's going wrong in production. Right now I don't see anywhere that you'd be logging the error, so you'll have an impossible time figuring out what's up.

Posted in Braintree Error In Production Mode

Hey Lee,

So for Braintree, you have to check _every_ time if the result is a success or not. In your case, it looks like the create of a customer may have failed.

          result = Braintree::Customer.create(
          email: current_user.company_email,
          company: current_user.company_name,
          payment_method_nonce: params[:payment_method_nonce]
          )
      
          customer = result.customer


This is the only line that calls `customer` on an object and can be the only line your original error came from. 

So you'll need to also handle the case where Braintree::Customer.create fails. You've got it handling the create Subscription failure, but not this one.

I'm not sure as to the redirect problem though.

Posted in Rails Application Templates Discussion

Actually, I bet it's just that you're not using the latest version of Rails 5.2 (rc1 and rc2 are what I'm using). I don't think the beta will work because it looks to have changed in this commit: https://github.com/rails/rails/commit/cf56397ccd10174d94f60331e4a55ff765b3485b

"gem install rails --pre" should fix your problem or just update it in your Gemfile to rc2.

Posted in Rails Application Templates Discussion

Hey Leonardo,

If the Procfile didn't exist, I would imagine that the full template didn't run successfully. I've made a handful of extra changes since the episode so you might want to try it again. I still haven't fixed the HTTP version of it so you'll need to use the downloaded repo still.

Posted in Rails Application Templates Discussion

Thanks for the heads up! I was working on overriding the scaffolds like I mentioned in the video and this was one of the errors I ran into. I think I already fixed it but forgot to push it up.

Posted in Sortable Drag and Drop Discussion

Thanks Joel!

Some answers to your questions (not exactly in order):

1. Rails.ajax is the new replacement for jquery's ajax ($.ajax) now that Rails no longer comes with jQuery. Rails.ajax is also smart enough to include the authenticity token in every request which is why you don't have to do #3. 

3. For your code, you should include the authenticity token in the request instead of disabling the check as you'll open yourself up to security vulnerabilities by turning it off.

2. jQuery-ui may have moved sortable to the widgets folder since I recorded the episode. Things are always changing so you'll often run into little changes like that.

Posted in Recurring events with the ice_cube gem Discussion

Good work! Looks like this probably changed somewhat recently then. The gem sure hasn't been updated for quite a while so it's surprising other things haven't broken!

Posted in Create form object with has_many attribute

This doesn't appear to be maintained anymore, but there is an ActiveModel Associations gem you could use. https://github.com/joker1007/activemodel-associations

I've used it in the past for a complex form object like you're doing and you could mix this in easily with Cocoon or something to build out the child fields_for sections.

Might be worth a shot!
I'll have to boot into Windows and see if I can replicate that. What a weird problem. I'm also possibly going to swap out Trix editor anyways because it's not the best for syntax highlighting.

There isn't really a Rails way of doing things because Rails refuses to get too involved in the client side JS. I came from Python before I started Ruby and had the same sort of struggle. It's not common to have a whole bunch of options to tackle things in other communities!
Hmm, spaces are working for me. Your post looks like you were in the code mode though.

You'll need an autocomplete or select library and then you'll need a JSON endpoint to gather the filtered records from. I did an episode on autocomplete here: https://gorails.com/episodes/global-autocomplete-search and I covered Selectize here: https://gorails.com/episodes/select-or-create-with-selectize-js

Selectize has an option for "remote data loading" as they call it. They have a couple demos at the bottom here that use Github and Rotten Tomatoes APIs. https://selectize.github.io/selectize.js/
Hey Taylor,

One-off charges are basically the same as Subscriptions. You just create a Stripe::Charge object instead of a Stripe::Subscription. 

1. Add Stripe to the Gemfile
2. Add the Stripe JS and checkout form to the page
3. The form for a new job should POST to the create action, so in there you can process the payment. 
4. Handle the payment and job creation together in a transaction

def create
  @job = current_user.jobs.build(jobs_params)
  if @job.create_with_stripe(params[:stripeToken])
    redirect_to @job
  else
    render 'new'
  end
end

class Job < ApplicationRecord
  def create_with_stripe(token)
    if valid?
      # Charge the user's card:
      charge = Stripe::Charge.create(
        :amount => 999,
        :currency => "usd",
        :source => token,
      )
      update(charge_id: charge.id)
    else
      false
    end
  rescue Stripe::CardError, Stripe::InvalidRequestError => e
    errors.add :base, "Whoops! We were unable to process your card. #{e.message}"
    false
  end
end

This way your payment logic is nicely organized with your Job. You check if the job is valid first, then we can fail quickly if it isn't valid. Then if it is valid, we can create the charge, and then update the Job with the charge ID for refund / reporting purposes. And if Stripe's charge fails with an error, we catch that and add the error to the Job and return false. Then your controller is nice and simple and just has to check if the create with stripe method returns true or false.

Posted in RailsConf

I was really wanting to, but I don't think I'll make it this year.

Posted in How to update TailwindsCSS?

Pretty much you'd just update the version in your package.json and use yarn to install it. You'll want to check with the releases page and see if anything has been deprecated or removed and upgrade your code if so. Other than that, I don't think there's much else you'd have to do.

Posted in Tools

New section I'm rolling out soon! Accidentally published it too soon. :) I wanted a place to recommend gems (like which file uploading tool should you pick?)

Posted in Deploy Ubuntu 16.04 Xenial Xerus Discussion

Hey Juan, I would probably just SSH in and run those commands on the server in the app's current directory. You probably could run Capistrano tasks to do that, but SSH is probably easier.