Generate a QR Code with Ruby on Rails

Tram Ho

Introduction

Have you ever thought of organizing a small 4.0 event? If you answered yes to this question, then you are most likely interested in making money and selling tickets. One way to verify that someone has purchased a ticket is via QR Code. Now I will guide you to build a Ruby on Rails web that can generate QR code using gem rqrcode

RQRCode is a library for creating and displaying QR codes in many different formats. It has a simple interface with all standard QR code options. It is adapted from the Javascript library by Kazuhiko Arase.

Installation

By default you already know how to install ruby ​​and rails.

Add the gem 'rqrcode' to your Gemfile in your app. Or you can install it manually using the command line gem install rqrcode .

Create a model called Ticket with attribute code and action show .

Setup route for Ticket: resources :tickets, only: :show

Basic usage example

Advanced Options

These are the different QR Code generation options provided by rqrqcode_core.

string – the string you wish to encode

size – the size of the qrcode (default 4)

level – the error correction level, can be:

  • Level: l 7% of code can be restored
  • Level: m 15% of code can be restored
  • Level: q 25% of code can be restored
  • Level: h 30% of code can be restored (default: h)

mode – the mode of the qrcode (defaults to alphanumeric or byte_8bit, depending on the input data):

  • : number
  • : alphanumeric
  • : byte_8bit
  • : kanji Example: qrcode = RQRCodeCore :: QRCode.new (‘VUONG’, size: 1, level:: m, mode:: alphanumeric)

Render types

Create sample data with seed:

as SVG

Basic

Rails

Share the news now

Source : Viblo