嗨,只是试图创建一个qr代码在我的rails网站上使用sam代码生成器https://github.com/samvincent/rqrcode-rails3.首先,我将这段代码添加到控制器中。
类QrcodeController < ApplicationController def qrcode respond_to do \x格式 format.html format.svg {呈现:qrcode => @qrurl,:level => :l,:unit => 10,:=> =>} format.png {呈现:qrcode => @qrurl } format.gif {呈现:qrcode => @qrurl } format.jpeg {呈现:qrcode => @qrurl } end def选项{: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。
发布于 2013-11-30 23:47:43
我用的是rqrcode码宝石。这非常简单,您不需要为您的qrcode生成图像。代码是使用表和一些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。
https://stackoverflow.com/questions/20304687
复制相似问题