I want to attach logo at the end of the email while sending mail . Below is my mailer code
employee_mailer.erb -
def user_confirmation(emp,pwd)
#password = pwd
#employee = emp
#emp = Employee.find_by(id: #employee.id)
mail(to: #emp.email, subject: 'HRMS Password Detail')
user_confirmation.html.erb -
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h4>Confirmation Email</h4>
<p>
<td> Welcome in HRMS of <%= #emp.company.name %> !!
This is to inform you that your default password is <b><%= #password %></b>
You can change your password.</td>
</p>
</body>
</html>
How I can send image as well with the above code .
You just need to add img tag as we do in HTML:
<img src="/location/image" alt="logo">
OR Rails way using Rails helper:
<%= image_tag("logo.png") %>
as you are sending a HTML email.
You can also add it as an inline attachment if required:
<%= image_tag attachments['logo.png'].url -%>
And in your mailer action:
attachments.inline['logo.png'] = File.read('path/to/logo.png')
On a Side Note:
I am not sure why you are doing this:
#employee = emp
#emp = Employee.find_by(id: #employee.id)
When you already have the employee in emp variable directly assign it to #emp or directly use #employee in which you have assigned it.
Hope this helps.
Related
I actually generate PDF with WickedPDF on my app. And now I just want to include an image "logo.jpg" on the header of my PDF.
I tried lot of things, and I think my problem come from assets.
My Controller :
def generate_pdf
pdf = WickedPdf.new.pdf_from_string(
render_to_string(
template: 'pdf/pdf_view.pdf.erb',
layout: 'layouts/application.pdf.erb'))
send_data(pdf,
filename: 'file_name.pdf',
type: 'application/pdf',
disposition: 'attachment')
end
layouts/application.pdf.erb :
<!doctype html>
<html>
<head>
<meta charset='utf-8' />
</head>
<body onload='number_pages'>
<div id="content">
<%= yield %>
</div>
</body>
</html>
pdf/pdf_view.pdf.erb :
<div>
<p>Just a simple text on my pdf that is correctly displayed</p>
<%= wicked_pdf_image_tag "images/logo.jpg", :width=>"250", :height=>"200" %>
</div>
And in my console :
Rendering pdf/pdf_view.pdf.erb within layouts/application.pdf.erb
Rendered pdf/pdf_view.pdf.erb within layouts/application.pdf.erb (2.7ms)
[wicked_pdf]: ["/home/vincent/.rvm/gems/ruby-2.7.1/bin/wkhtmltopdf", "file:////tmp/wicked_pdf20210503-14279-o2q5ol.html", "/tmp/wicked_pdf_generated_file20210503-14279-1om4oam.pdf"]
source=rack-timeout id=cfb572c3-5919-47c1-b32c-cc3bf724b9bf timeout=15000ms service=1000ms state=active
Rendering text template
Rendered text template (0.1ms)
Sent data file_name.pdf (1.5ms)
Completed 200 OK in 1140ms (Views: 1.1ms | ActiveRecord: 1.1ms)
So my question is : Do you have an other method to display an image on a generated pdf ? Or is my method wrong ?
Problem solved, the right command to display my images on the pdf is :
image_tag wicked_pdf_asset_base64
There is my jsp page in which a href links are there.
So i have to redirect it to that particular web page when i click to that link.
The links are fetched from my database.
Below is my jsp page
<%# page import="java.sql.*" %>
<%ResultSet rs=null; %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>
Your Searched Results Are :
</title>
<link href="Desktop/style.css" rel="stylesheet" type="text/css" />
</head>
<body bgcolor="8B4513">
<%
Connection conn=null;
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn=DriverManager.getConnection
("jdbc:mysql://localhost:3306/tendermysql","root","root");
Statement stmt=conn.createStatement();
rs=stmt.executeQuery("select * from Record");
%>
<form>
<center>
<h1> Welcome to Ezest Tender Optimzed Search</h1>
<%
while(rs.next())
{
%>
<a href= "<%rs.getString(2); %>"
onclick = "window.location.href=this.form.rs.getString(2)
[this.form.rs.getString(2)]" > <% out.println(rs.getString(2)); %>
</a>
<br />
<% } %>
<% }
catch(Exception e)
{
out.println("Wrong Input" +e);
}
%>
<br>
</center>
</form>
</body>
</html>
I tried it but the page is not redirecting...
Porblem here is that your ResultSet is forward only so you cannot use this line rs.getString(2) to get same record more than once inside loop. Use a temp variable to repeat the value. Also there is no need to give onclick attribute when href is given the same link.
String str;
while(rs.next()){
str = rs.getString(2);%>
<%=str%><%
}
Sample code with href and onclick:
<a href='http://stackoverflow.com/' onclick='return confirm("Visit page?")'>Stackoverflow</a>
P.S. Make sure to close db connection
I'm creating an ASP.net website and I need to use a VBScript to handle my code using a function so that every time i call that function it writes that code in html.
Something like this:
function heading(tite){
<html>
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso8859-1">
<title><%= title %></title>
</head>
}
How can i do this?
Thanks
<% sub heading(titl) %>
<html><head>your html here<% =titl %>
</head>
<% end sub %>
</html>
If I have an .html.erb file that looks like this:
<html>
<head>
<title>Test</title>
</head>
<body>
<%= #name %>
</body>
</html>
How can I generate an HTML file that looks like this?
<html>
<head>
<title>Test</title>
</head>
<body>
John
</body>
</html>
How can I execute the template (passing the name parameter) given that the format is .html.erb and be able to get just an .html file?
#page.html.erb
<html>
<head>
<title>Test</title>
</head>
<body>
<%= #name %>
</body>
</html>
...
require 'erb'
erb_file = 'page.html.erb'
html_file = File.basename(erb_file, '.erb') #=>"page.html"
erb_str = File.read(erb_file)
#name = "John"
renderer = ERB.new(erb_str)
result = renderer.result()
File.open(html_file, 'w') do |f|
f.write(result)
end
...
$ ruby erb_prog.rb
$ cat page.html
<html>
<head>
<title>Test</title>
</head>
<body>
John
</body>
</html>
Of course, to make things more interesting, you could always change the line #name = "John" to:
print "Enter a name: "
#name = gets.chomp
The ruby ERB library operates on strings, so you would have to read the .erb file into a string, create an ERB object, and then output the result into an html file, passing the current binding to the ERB.result method.
It would look something like
my_html_str = ERB.new(#my_erb_string).result(binding)
where #my_erb_string. You could then write the html string into an html file.
You can read more about binding here and about the ERB library here.
Here is a sample code:
require 'erb'
template = File.read("template.html.erb")
renderer = ERB.new(template)
class Message
attr_accessor :name
def initialize(name)
#name = name
end
def get_binding
binding()
end
end
message = Message.new 'John'
File.open('result.html', 'w') do |f|
f.write renderer.result message.get_binding
end
The template.html.erb is your .html.erb template file and result.html is the rendered html file.
Some nice articles you can take a look, link1, link2.
When I view my Rails (3.1) app in Internet Explorer, for some reason it sometimes shows bits of html tags at the end of my page, but it doesn't do this in Chrome or Firefox. In particular, it shows part of the tag - never the full tag, but part - like </h or </htm - in the view as text (at the bottom of the page).
In my layout template, I do have this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><%= title %></title>
<% if params[:controller] == 'pages' %>
<%= stylesheet_link_tag "static_pages", "https://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/redmond/jquery-ui.css" %>
<% elsif %>
<%= stylesheet_link_tag "bootstrap", "https://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/redmond/jquery-ui.css" %>
<% end %>
<%= javascript_include_tag "https://ws.sharethis.com/button/buttons.js", "admin.js" %>
<%= csrf_meta_tag %>
<script type="text/javascript" charset="utf-8">
//jQuery.noConflict();
jQuery.extend( jQuery.fn.dataTableExt.oStdClasses, {
"sSortAsc": "header headerSortDown",
"sSortDesc": "header headerSortUp",
"sSortable": "header"
} );
jQuery(document).ready(function() {
jQuery('#datatable').dataTable( {
"sDom": "<'row'<'span5'l><'span8'f>r>t<'row'<'span4'i><'span8'p>>",
"sPaginationType": "bootstrap"
} );
} );
</script>
<%= yield :javascript%>
<%= yield :script%>
</head>
<body>
</body>
</html>
When I look at the html source code generated for the view, I notice that the Chrome/Firefox the source concludes with </body> and not </html> (or any partial code like IE).
Can someone please let me know what I'm doing wrong? Thank you.
An empty Head tag will not be recognized as well formed (closed ) unless it contains a mandatory child
( as defined in the DTD of the doctype 'Transitional' you are using. )
Give it a title
<head>
<title>A Title</title>
</head>
I think this is mandatory for all doctypes ( but don't quote me on that ) - but mute point too - every web page should have a title.
Update: Says title is present, still sounds like a "well formed" issue though ..
1) Run, view source and copy all the code into notepad/simular and save that as test.html ( this will highlight any chars that are not correct like ( " ) etc ..
2) Upload your test.html into the w3c validator service w3c Validator service
That should give some good clues
hope that cracs it