ruby on rails qr code implementmetation

 小何775 发布于 2023-02-13 17:05

嗨,我试图在我的rails网站上使用sam vincents qr代码生成器创建一个qr代码https://github.com/samvincent/rqrcode-rails3 .......首先我将此代码添加到控制器

class QrcodeController

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 

结束

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

Qr code

<%= link_to "SVG", Qrcode_path("svg") %>

<%= link_to "PNG", Qrcode_path("png") %>

<%= link_to "JPEG", Qrcode_path("jpeg") %>

<%= link_to "GIF", Qrcode_path("gif") %>

我会很感激任何有关它如何工作的帮助,因为他们使用ruby 1.9.3和rails 4.0.1并不是很多在线指令

1 个回答
  • 我正在使用rqrcode gem.它非常简单,您不需要为qrcodes生成图像.代码是使用表格和一些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.

    2023-02-13 17:06 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有