Ruby on rails qr code implemetation

Hi, just trying to create qr code on my rails website using the qr code generator. https://github.com/samvincent/rqrcode-rails3 ....... first I added this code to the controller

class QrcodeController <ApplicationController

def qrcode reply_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'm not sure what to add to 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 this works, as there are not many instructions on the Internet im using ruby โ€‹โ€‹1.9.3 and rails 4.0.1

+6
source share
1 answer

I am using rqrcode gem. It is quite simple and you do not need to create 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 

In your opinion some_view.html.erb

 <%= render_qr_code("MYCODE") %> 

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

 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 } } } 

In my example, it works with Rails 3.

+7
source

Source: https://habr.com/ru/post/959091/


All Articles