Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serving the PNG file in a Rails view? #1

Closed
mrichman opened this issue Oct 8, 2011 · 1 comment
Closed

Serving the PNG file in a Rails view? #1

mrichman opened this issue Oct 8, 2011 · 1 comment

Comments

@mrichman
Copy link

mrichman commented Oct 8, 2011

How would I serve up the generated PNG file from a Rails view?

This works using the HTML table method described in rqrcode, but I'd rather use your PNG version:


require 'rqrcode'

class UsersController < ApplicationController
  before_filter :authenticate_user!

  def show
    @user = User.find(params[:id])
    @qr = RQRCode::QRCode.new('http://www.google.com/')
  end

end

<style type="text/css">
 table {
  border-width: 0;
  border-style: none;
  border-color: #0000ff;
  border-collapse: collapse;
 }
 td {
   border-width: 0;
   border-style: none;
   border-color: #0000ff;
   border-collapse: collapse;
   padding: 0;
   margin: 0;
   width: 10px;
   height: 10px;
 }
 td.black { background-color: #000; }
 td.white { background-color: #fff; }
</style>

<table>
 <% @qr.modules.each_index do |x| -%>
   <tr>
   <% @qr.modules.each_index do |y| -%>
    <% if @qr.dark?(x,y) -%>
    <td class="black"/>
    <% else -%>
    <td class="white"/>
    <% end -%>
   <% end -%>
   </tr>
 <% end -%>
</table>

@DCarper
Copy link
Owner

DCarper commented Oct 8, 2011

Depends on the situation. If the QR codes are non-dynamic then you could just generate them in development and serve them like normal images.

If you're creating dynamic QR codes then you should be able to use an attachment handling gem like Dragonfly or PaperClip.

With Dragonfly it might look something like this ( I won't have time to test it until later )

@my_model.qr_code_att = RQRCode::QRCode.new('http://www.google.com/').to_img.to_string
# if that doesn't work
@my_model.qr_code_att = RQRCode::QRCode.new('http://www.google.com/').to_img.save("#{Rails.root}/tmp/yada.png")

Like I said I won't have time to test things until later. This was a good question and at the very least I'll be updating the docs with info.

Dan

@DCarper DCarper closed this as completed Oct 14, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants