my problem is to get my QRCode to display in the email sent via action mailer in rails.
I am currently using the gem 'rqrcode_png'
I have read through some guides and it states inline attachment. However, I did not save the QRCode generated in my own files.
Is there a way to display the QRCode in the email without having to save it in my database?
Also, I have tried the following code on my view page and it works, but it does not work when I copy it to my action mailer code. It only shows the QR Code for venue.
Here is my code below for the displaying of QRCode. Thank you!
<p>
<% @deal.venues.each do |venue| %>
<strong>QR Code for <%= venue.neighbourhood %></strong><br>
<% @qr = RQRCode::QRCode.new(@deal.id.to_s + "_" + venue.id.to_s + "_" + @deal.created_at.to_s).to_img.resize(100, 100).to_data_url %>
<span><%= image_tag @qr %><br></span>
<% end %>
</p>
Solution by Lewis Buckley with some edits
I made some edits to the code and now it is working. As, I am using ruby i have to place <%= %> around the variables in order to display them.
<img src="https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=<%= @deal.id.to_s + '_' + venue.id.to_s + '_' + @deal.created_at.to_s%>" alt="QR code">
I'm not sure about your exact use case but I have used the Google Chart QR code generator before and it is super simple (you need to url encode your content with the u
method):
<img src="https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=#{u(@deal.id.to_s + "_" + venue.id.to_s + "_" + @deal.created_at.to_s)}&choe=UTF-8" alt="QR code">
And then just copy that into your mailer 'view'.
Define @url in the controller. Here's examples to linkto for debugging and the img source for displaying. The above example used url encoding for & and this didn't work for me. Use & for the urls.
<%= link_to("https://chart.googleapis.com/chart?chs=545x545&cht=qr&chl=#{@url}&choe=UTF-8","https://chart.googleapis.com/chart?chs=545x545&cht=qr&chl=#{@url}&choe=UTF-8") %>
<img src="https://chart.googleapis.com/chart?chs=545x545&;cht=qr&;chl=#{@url}&;choe=UTF-8" alt="QR code">
chs=548x547 is the max allowed for size, which you'll need for larger urls.
来源:https://stackoverflow.com/questions/32538786/rails-4-getting-qrcode-to-display-in-email-via-action-mailer