1
0

More than 5 years have passed since last update.

Padrino使ってQRcodeを表示させる

Last updated at Posted at 2013-03-14

準備

$ padrino g project testqr -e slim
$ cd testqr
$ bundle install

Gemfile

以下をGemfileに追記

gem 'rqrcode'
gem 'rqrcode_to_svg'

追記後にbundle installを行うこと

作成

コントローラーの作成

$ padrino g controller qrtest get:index get:code
app/controller/qrtest.rb
Testqr.controllers :qrtest do

  get :index, map: "/index" do
    render "qrtest/index"
  end

  get :code, map: "/code" do
    RQRCode::QRCode.new('http://www.co-meeting.com', size: 4, level: :h ).to_svg
  end
end

ビューの作成

app/view/layout/qrtest/index.slim
object type="image/svg+xml" data="code"

説明

padrino startでサーバを起動させる。

http://localhost:3000/qrtest/indexにアクセスすると、QRコードが表示されるはず。

テンプレートファイルにあるobjectタグがポイントで、data属性でURLを指定している。その結果、qrtest/codeへリクエストが発生し、to_svgでSVGに変換されたQRコードのデータがブラウザへ返信されることでレンダリングが行われる。

ChromeなどのSVGによるレンダリングが可能なブラウザでは、SVGデータを送ってあげればQRコードがレンダリングされる。
SVGが使えない場合は、PNGなどへの出力を行うことを検討しよう。

参照元URL

RQR Code
https://github.com/whomwah/rqrcode

RQR Code to SVG
https://github.com/verisimilitudinous/rqrcode_to_svg

スタートアップ SVG「第1回 SVGの基礎知識」
http://gihyo.jp/dev/feature/01/svg/0001

備考

  • ブラウザはChromeなどで見てください。SVGのレンダリングが行えるブラウザでしか見られません。
  • Ruby2.0.0-p0で動作を確認しています。
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0