Rails 4: Getting QRCode to display in email via action mailer

假装没事ソ 提交于 2019-12-08 04:28:36

问题


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&amp;cht=qr&amp;chl=<%= @deal.id.to_s + '_' + venue.id.to_s + '_' + @deal.created_at.to_s%>" alt="QR code">

回答1:


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&amp;cht=qr&amp;chl=#{u(@deal.id.to_s + "_" + venue.id.to_s + "_" + @deal.created_at.to_s)}&amp;choe=UTF-8" alt="QR code">

And then just copy that into your mailer 'view'.




回答2:


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 &amp; 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!