【问题标题】:ruby on rails qr code implemetationruby on rails 二维码实现
【发布时间】:2013-12-16 18:25:12
【问题描述】:

您好,我只是想在我的 Rails 网站中使用 sam vincents 二维码生成器 https://github.com/samvincent/rqrcode-rails3........ 首先我将此代码添加到控制器中

类 QrcodeController

定义二维码 respond_to 做|格式|
格式.html
format.svg { render:qrcode => @qrurl, :level => :l, :unit => 10, :color => black }
format.png { 渲染 :qrcode => @qrurl } format.gif { 渲染 :qrcode => @qrurl } format.jpeg { 渲染 :qrcode => @qrurl } 结尾 结束

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

结束

然后我不确定在我尝试过的视图中添加什么

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

希望对它的工作原理提供任何帮助,因为它们的在线说明并不多 我正在使用 ruby​​ 1.9.3 和 rails 4.0.1

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 qr-code


    【解决方案1】:

    我正在使用rqrcode gem。这非常简单,您不需要为您的二维码生成图像。代码是使用表格和一些css样式生成的...

    你可以使用这个助手:/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
    

    进入你的视野some_view.html.erb

    <%= render_qr_code("MYCODE") %>
    

    你需要为你的代码添加样式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
        }
      }
    }
    

    我的例子是使用 Rails 3。

    【讨论】:

    • 我一直在做类似的事情。一个问题:当我打印/保存页面时,模块之间会出现细白边框线......可能只是我的 PDF 查看器
    • 另外值得注意的是,当您有较长的文本要编码时,这是 rqrcode 的常见问题github.com/whomwah/rqrcode/issues/15
    • 但是您在视图中的何处添加代码以显示 .png?
    猜你喜欢
    • 1970-01-01
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    相关资源
    最近更新 更多