7

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

Sarah Duffy
  • 191
  • 3
  • 14

2 Answers2

9

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.

Leantraxxx
  • 4,506
  • 3
  • 38
  • 56
  • I've been doing similar. One issue: When I print/save the page, thin white border lines show up between the modules... maybe just my PDF viewer – Don Cheadle Feb 24 '15 at 16:58
  • also worth noting that this is a common problem with rqrcode when you have longer text to encode https://github.com/whomwah/rqrcode/issues/15 – Don Cheadle Feb 24 '15 at 18:18
  • But where do you add the code in your view to display a .png? – Matt P Dec 28 '17 at 14:23
1

Here's a minimal example that avoids the need to write code to render each (x,y) "pixel" yourself.

In your controller:

def show
    @qr = RQRCode::QRCode.new('https://stackoverflow.com/')
end

And then in your corresponding .html.erb view:

<p>
    <%== @qr.as_svg %>
</p>

(Note that this uses <%==, not <%=; if you use the latter, you'll get the raw SVG XML source instead of a rendered SVG image.)

See the RQRCode docs for options that can be passed to .as_svg, and for other output options such as PNG.

Jon Schneider
  • 25,758
  • 23
  • 142
  • 170