嗨,我试图在我的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 enddef 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 回答手机用户2502855967撰写答案