42
loading...
This website collects cookies to deliver better user experience
# Gemfile
gem 'rqrcode'
bundle install
.# config/routes.rb
controller :pages do
get :qr_code_generator
get :qr_code_download
end
# app/controllers/pages_controller.rb
class PagesController < ApplicationController
def qr_code_generator; end
def qr_code_download
send_data RQRCode::QRCode.new(params[:content].to_s).as_png(size: 300), type: 'image/png', disposition: 'attachment'
end
end
<%# app/views/pages/qr_code_generator.html.erb %>
<h1>QR code generator</h1>
<%= form_tag :qr_code_download, method: :get do |f| %>
<div class="field">
<%= label_tag 'Text or URL' %>
<%= text_field_tag :content, nil, required: true %>
</div>
<div class="actions">
<%= submit_tag 'Generate QR code' %>
</div>
<% end %>
qr_code_download
which actually generates a PNG for the QR code. The QR code will be downloaded on the user device since we use disposition: 'attachment'
.qr_code_download
but with disposition: 'inline'
.