Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby on rails qr code implemetation

hi just trying to create a qr code in my rails website using sam vincents qr code generator https://github.com/samvincent/rqrcode-rails3....... first i added this code to a controller

class QrcodeController < ApplicationController

def qrcode respond_to do |format|
format.html
format.svg { render:qrcode => @qrurl, :level => :l, :unit => 10, :color => black }
format.png { render :qrcode => @qrurl } format.gif { render :qrcode => @qrurl } format.jpeg { render :qrcode => @qrurl } end end

   def options 
     {:qrcode => "http://helloworld.com", size => 4} 
      end 

end

then i am not sure what to add in the view i tried this

<div class="Qrcode qr">
<h2>Qr code</h2>

<p><%= link_to "SVG",  Qrcode_path("svg")  %></p>
<p><%= link_to "PNG",  Qrcode_path("png")  %></p>
<p><%= link_to "JPEG", Qrcode_path("jpeg") %></p>
<p><%= link_to "GIF",  Qrcode_path("gif")  %></p>

would appreciate any help on how it works as their are not that many instructions online im using ruby 1.9.3 and rails 4.0.1

like image 674
Sarah Duffy Avatar asked Nov 30 '13 19:11

Sarah Duffy


People also ask

How much does it cost to implement QR code?

Static QR Codes are free, whereas dynamic QR Codes cost as low as $5 a month.


1 Answers

I'm using rqrcode gem. It's pretty simple and you don't need to generate images for your qrcodes. The code is generated using tables and some css styles...

You can use this helper: /helpers/qrcode_helper.rb

module QrcodeHelper
  require 'rqrcode'

  def render_qr_code text, size = 3
    return if text.to_s.empty?
    qr = RQRCode::QRCode.new(text)
    sizeStyle = "width: #{size}px; height: #{size}px;"

    content_tag :table, class: "qrcode pull-right" do
      qr.modules.each_index do |x|
        concat(content_tag(:tr) do
          qr.modules.each_index do |y|
            color = qr.dark?(x, y) ? 'black' : 'white'
            concat content_tag(:td, nil, class: color, style: sizeStyle)
          end
        end)
      end
    end
  end
end

Into your view some_view.html.erb

<%= render_qr_code("MYCODE") %>

And you need to add style for your code qrcode.css.less

table.qrcode {
  border-width: 0;
  border-style: none;
  border-color: #0000ff;
  border-collapse: collapse;
  margin-top: 100px;
  margin-bottom: 12px;

  td {
    border-width: 0;
    border-style: none;
    border-color: #0000ff;
    border-collapse: collapse;
    padding: 0;
    margin: 0;
    width: 3px;
    height: 3px;

    &.black {
      background-color: #000 !important
    }

    &.white {
      background-color: #fff !important
    }
  }
}

My example it's working with Rails 3.

like image 145
Leantraxxx Avatar answered Sep 20 '22 20:09

Leantraxxx