Daniel Weaver

Joined

4,440 Experience
13 Lessons Completed
2 Questions Solved

Activity

Posted in Parsing a Ruby hash sent to a Javascript API

Sending data from Rails via HTTParty to a Node endpoint:

params = { str: "word", bool: true, hash: { number: 123 } }
result = HTTParty.get("http://localhost/test.js", query: params)

It works fine without the nested hash in the params but when I add that in things get hairy.

If I send it as shown above the Node endpoint receives:
GET /test.js?str=word&bool=true&hash[number]=123 and hash is undefined

If I convert the nested hash to a string:
params = { str: "word", bool: true, hash: { number: 123 }.to_s } then Node receives this:
GET /test.js?str=word&bool=true&hash=%7B%3Anumber%3D%3E123%7D and hash is available in JS as: "{:number=>123}" but I can't figure out how to convert this to a JS object.

If I convert the nested hash to query:
params = { str: "word", bool: true, hash: { number: 123 }.to_query } then Node receives this:
GET /test.js?str=word&bool=true&hash=number%3D123 and hash is available in JS as: "number=123".

How do I access that nested hash in JS? How do I convert it to a usable object? I don't mind which of the various ways to send the nested params, I just to access them easily.

Excellent, cheers. Good time for me to dive into Simulus then! 😀

Right, Javascript does sound like the better route. Would you hijack the form submit and set the values there? Or would you respond to changes on the radio button inputs as they happen then let the form submit normally?

I have a spectrum boolean column and a spectrum_color string column and I'd like to display the options to user as shown in this image. I'm using Tailwind CSS and the Jumpstart Pro Rails template so the visual side is easy, just not sure what form helpers to use to get the values from 3 radio buttons to fill one boolean field and a string field.

EDIT: can't get an image to display in this post. Here's a link: https://wmd.d.pr/AMZFsu

Posted in Is this a good fit for storing JSON in database?

Thanks Chris. I found your video on this right after I posted. https://gorails.com/episodes/preferences-settings-with-activerecord-store

Looking at the typed_store gem now 👍 https://github.com/byroot/activerecord-typedstore

Posted in Is this a good fit for storing JSON in database?

I've got a Video model that represents a video created by the user. During creation of a video a couple of filters with different options can be applied to the video. Currently only 2 but I'll be adding more filters soon.

Would this be a good case for storing the filter options in the database as a single JSON column in the databse (Postgres)?

At first I thought of adding boolean and string/integer columns for each filter (and filter option) but even with just 2 filters this gets pretty verbose:

spectrum: boolean => true/false
spectrum_color: string => white/black
spectrum_width: string => thin/think
spin: boolean => true/false
spin_radius: integer => 200/400/800

Seems much better to store this as a JSON object. Any gotchas I should look out for?

Posted in Where to read about changes between Ruby versions?

Tips for updating Ruby versios? Is there somewhere to look for human-readable changes made between Ruby versions?

I've looked at the Github diffs between Ruby versions which is fine but it's not very readable and can take a long time to parse: https://github.com/ruby/ruby/compare/v2_6_3...v2_6_4

Posted in Grandfathering

By the way, I can attest to the GoRails old plans because I was on the $9/month plan back in 2014 but like a fool I canceled a couple of years later only to resubscribe recently at $19/month! Doink! 😀

Posted in Grandfathering

I have the same situation with klippr.co - it's free to use at the moment but I plan to add a paid plan as I introduce new features.

The app allows users to create short videos from music tracks. I'm considering two ways to transition to paid: [1] let the free users stay on the free plan but they don't get any of the new features, only paid users get those, or [2] let the free users have all the new features but they have a watermark on their videos and can remove the watermark by subscribing.

Alternatively I might do similar to what you're doing - let all the existing free users stay free and get all the features but remove the free plan from the site so all new users must subscribe to a paid plan.

Lots to think about! 😀

Posted in Jumpstart

Although it might not handle the marketplace features you need right now I can higly recommend Jumpstart Pro for the time it'll save in other areas - users, teams, authentication, social login, announcements, emails, user impersonation, and a great CSS base design 👍

Posted in Grandfathering

We've done similar in one of our apps. You can end up in a bit of a mess in Stripe if you're not careful about naming.

For instance, we had small, medium and large plans but I since learned to affix the price to the plan ID so that when the price changes it's still obvious which is which. So now we have small-49, medium-99 and large-199.

If we change prices in the future I can just created another set of plans like small-69, medium-129 and large-249 and leave the existing plans there. Easy!

Posted in Streaming remote files to S3 🤔

For each user request I need to save two remote files to AWS S3. I have the remote URLs of each - one image JPG ~100KB and one audio MP3 ~1.5MB.

I know I can do this by downloading each file to temp then uploading to S3 using the aws-sdk gem. However I'm concerned about performance. The files aren't exactly huge but if I'm doing this often enough will it cause an IO bottleneck on my server?

Has anyone done this with streaming? There must be a way to open the remote file as a stream and send that stream direct to S3 without it being saved on my server disk? I assume this would all happen in memory. In fact, is that any less of a bottleneck than saving to temp?

Confused! Help! 😂

Posted in How to model these relations in Rails

Have you looked at Multiple Table Inheritance? Seems like it might work.

This gem makes it really easy: https://github.com/krautcomputing/active_record-acts_as

Posted in How to get ngrok working with webpack-dev-server

I run my local dev site on ngrok and the webpack-dev-server spits out errors to the console on a regular basis:

GET https://localhost:3035/sockjs-node/info?t=1566173800826 net::ERR_CERT_AUTHORITY_INVALID @ sockjs.js:1795

[WDS] Disconnected!

Any ideas what I need to change to stop these errors appearing? Nothing I've tried so far has worked. Or do I need to make choice between using webpacker and ngrok?

Got this working by parsing and opening the URL with URI.parse:

def video_email
  @user = params[:user]
  @video_url = params[:video_url]
  attachments['video.mp4'] = File.read(URI.parse(@video_url).open)
  mail(to: @user.email, subject: 'Your video is attached!')
end

Not sure why it wasn't working with a regular string. The OpenURI docs seem to suggest a string will work.

No, this is a video file that I'm generating in a function outside of Rails.

Eventually I'll be pulling the function into Rails so the S3 file will be easier to handle but for now it's a totally separate file.

I'm using a regular mailer to send text emails and now I want to attach a file stored on S3 to the email. I have the URL of the file and was naively hoping something like this would work:

def video_email
  @user = params[:user]
  @video_url = params[:video_url]
  attachments['video.mp4'] = File.read "https://me.s3.com/video.mp4"
  mail(to: @user.email, subject: 'Your video is attached!')
end

But I get an error:

Errno::ENOENT: No such file or directory @ rb_sysopen - "https://me.s3.com/video.mp4"

What's the best way for me to attach a remote file to an outgoing email?

Posted in How to protect API againest malicious attack?

To clarify - the request is being built in the browser with the API token exposed, correct?

And you're concerned about this API token being visible to anyone that pokes around in the browser dev tools?

Seems like this kind of request would be better built server-side so the API token is protected. The website would send the contents of the hash back to the server then the server would build the full requests and send it to your API.

But aybe you don't have control over the client side of things?

Posted in Need a hand with associations

Can a user be both a regular user and the ower of an account at the same time?

logo Created with Sketch.

Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

© 2020 GoRails, LLC. All rights reserved.