How to use TCPServer in Ruby on Rails? - ruby-on-rails

I am using Ruby on Rails on Cloud9 IDE
$ ruby -v
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]
$ rails -v
Rails 4.2.4
I have to transmit data between server and GPS device. I put on device and did code like below.
My_Controller
class DashboardController < ApplicationController
before_action :authenticate_user!
def index
require 'socket'
server = TCPServer.new ("127.0.0.1",8000)
loop do
Thread.start(server.accept) do |client|
client.puts "Hello !"
client.puts "Time is #{Time.now}"
client.close
end
end
#bookings = Booking.where(user: current_user).joins(:customer, :driver, :car)
#customers = Customer.where(user: current_user)
end
end` ****
when i ran this i got
<html>
<head>
<meta charset='utf-8'>
<title>Error 502 - Bad Gateway</title>
<link rel="stylesheet" type="text/css" href="https://cdn.c9.io/errors/style.css" />
<style type="text/css">
.error_content {
background: rgba(255, 255, 255, 0.23);
padding: 10px;
width: 641px;
margin: 25px 0;
display: none;
}
#error-msg {
display: block;
}
</style>
</head>
<body class="errorUnknown light">
<div id="wrapper">
<h1>Error 502 - Bad Gateway</h1>
<div class="error_content" id="error-msg">
<p>Please click here to try again, if the issue persists please contact support</p>
</div>
Status Page |
Support |
Dashboard |
Home
</div>
</body>
</html>
Kindly, help me how to solve this problem..
Thanks in advance.

You go into an endless loop in the action. The request from the reverse proxy to your rails server times out. The reverse proxy sends an error message (502) to the browser.

Related

ActionMailer email preview is not the same as one user receives in production

I am using Rails 6 and ActionMailer to send an email when the user joins. It is also probably very important to mention that I use API only application and I host it on Heroku.
I have an .html.erb view setup and it looks like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<div style="width: 100%;display: flex;flex-direction: column;align-items: center;padding: 15px;background-color: white;font-family: 'Helvetica';color: #2c3e50">
<h1>Welcome to company!</h1>
<div style="height: 1px;background-color: dimgrey;width: 50%;margin-top: 10px; margin-bottom: 20px;"></div>
<h4 style="margin:0">Want to learn more about our platform?</h4>
<p style="margin-bottom: 30px">Check out this quick video guide.</p>
<h4 style="margin:0">Join our Discord server</h4>
<h4 style="margin:0">Follow us on social media</h4>
<p style="margin-bottom: 30px">Some icons with social media</p>
<%= link_to "Create Password", #url, style: "color: white;padding:10px;border-radius:10px;margin-bottom:20px" %>
</div>
</body>
</html>
Now, this code in preview would give this kind of view
And that is good, that is what I want.
But when user receieves it in production it looks something like this
Am I using something that I shouldn't? How do I make it look like it looks like in the preview.
Here are some more files that might interest you:
Stuff I added to production.rb regarding the mailer:
config.action_controller.asset_host = 'https://myapponheroku'
config.action_mailer.asset_host = config.action_controller.asset_host
config.action_mailer.perform_caching = false
config.action_mailer.delivery_method = :smtp
host = 'myapponheroki'
config.action_mailer.default_url_options = { host: host }
# smtp settings here
Do I have to add anything else here to make it work?
My mailer is located in app/views/user_mailer
Please ask if you need any more code snippets or information.
What email client was used for that screenshot? Could just be an email client issue and flex. Maybe to debug, you could change your view to use just a regular table and see if that fixes the formatting.
Also, I have used these in the past with success. They usually are handled by most major email clients pretty well.
https://litmus.com/community/templates

Graph API errors that we cannot control

We are working on an automation that reaches out to Graph API on a schedule (every 5 minutes) and manipulates some worksheets.
Most of the times it works fine, but sporadically, we're running into a errors coming from the API that we cannot control, here's the most frequent:
Status: 504
cannot POST /v1.0/me/drive/items/<docid>/workbook/createSession (504)
Is this a known issue? Or are we doing something wrong?
And sometimes, the error comes in a totally bizarre way, as html:
StatusError: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/> <title>404 - File or directory not found.</title> <style type="text/css"> <!-- body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;} fieldset{padding:0 15px 10px 15px;} h1{font-size:2.4em;margin:0;color:#FFF;} h2{font-size:1.7em;margin:0;color:#CC0000;} h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} #header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF; background-color:#555555;} #content{margin:0 0 0 2%;position:relative;} .content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;} --> </style> </head> <body> <div id="header"><h1>Server Error</h1></div> <div id="content"> <div class="content-container"><fieldset> <h2>404 - File or directory not found.</h2> <h3>The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.</h3> </fieldset></div> </div> </body> </html> at s (/var/task/dist/index.js:1:8872) at <anonymous> at process._tickDomainCallback (internal/process/next_tick.js:228:7) name: 'StatusError', status: 404, code: 'UnknownError', agent: 'cannot POST /v1.0/me/drive/items/01MFBSIOQF7YVAPATDUJEI2LJAMXXGERX3/workbook/createSession (404)' }

Rails Internal Style For Mailer Template Not Working

Internal style written in rails mailer template is not working.
<html>
<head>
...
...
</head>
<body>
<style type="text/css">
...
...
</style>
</body>
</html>
How to solve this problem?
Make sure that you are putting internal style inside head tag
eg:
<head>
<style type="text/css">
...
...
</style>
</head>
Note: Only internal style (must be in head tag) supports.

CSS Images in Email With Rails 3

I'm trying to send out an email with Rails 3 and Action Mailer. The email goes out fine, but I want it to be HTML formatted with some basic styling which includes background images. I understand that the images might get blocked until the user allows them to be shown, but I still think it would be best to link to the images on my web server.
The email template called registration_confirmation.html.erb starts out like 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>Untitled Document</title>
<style type="text/css">
body {
background: url(/images/mainbg_repeat.jpg) top repeat-x #cfcfcf;
margin: 0px 0px 0px 0px;
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
color: #565656;
}
What is the best way to get the url link for the background image to have the full host included so that the background will show up in the email?
In response to my other answer, #Kevin wrote:
Thanks for the answer, I did think of doing something like that but I
don't think it's possible with the way I have it setup. The mailer
call is happening in a after_create call in a model rather than in a
controller so I don't think I have access to the request object as you
mentioned (or am I mistaken). I do have this in my mailer initializer:
ActionMailer::Base.default_url_options[:host] = "localhost:3000" Can I
somehow use that hos parameter in my mailer to make it work?
The answer is yes. All rails url-construction helpers should use these defualt_url_options. In addition to setting the :host, you should also force it to use absolute urls by settings this option:
ActionMailer::Base.default_url_options[:only_path] = false
Also, set the asset host like this:
config.action_mailer.asset_host = 'http://localhost:3000'
Then just use the image_path helper instead of hand-writing the url, like this:
<style type="text/css">
body {
background: url(<%= image_path('mainbg_repeat.jpg') %>) top repeat-x #cfcfcf;
}
</style>
NOTE: Setting default_url_options directly like that is deprecated. Here's the new way to do it:
config.action_mailer.default_url_options = {
:host => 'localhost:3000',
:only_path => false
}
Pass your request host as a parameter to the mailer method, and then pass it from the method to the view. So, for example, your mailer method might look like this (example lifted from rails docs and modified here):
class UserMailer < ActionMailer::Base
default :from => "notifications#example.com"
def registration_confirmation(user, host)
#user = user
#host = host
mail(:to => user.email, :subject => "Welcome to My Awesome Site")
end
end
You would call it like this:
def some_action
UserMailer.registration_confirmation(#user, request.host).deliver
end
Then in your view, you would just use the #host:
<style type="text/css">
body {
background: url(http://<%= #host %>/images/mainbg_repeat.jpg) top repeat-x #cfcfcf;
}
</style>
This is all assuming the image server is the same as the server running the request. If the image server is hosted elsewhere, you have to output a constant here. You could put something like this in lib/settings.rb:
module Settings
IMAGE_HOST = 'superawesome.images.com'
end
Then in your view, you'd just output the constant there, like this:
<style type="text/css">
body {
background: url(http://<%= Settings::IMAGE_HOST %>/images/mainbg_repeat.jpg) top repeat-x #cfcfcf;
}
</style>
If you do not care about performance, the roadie gem can handle urls in the stylesheets for you.

Export To Excel - Change Default "Save as type" to Microsoft Office Excel Workbook(*xls)

I am Doing an Export to Excel with my Ruby on Ralis application.I have not used any gem or Plugin (since our requirement was something different, we couldn't use them). We used the inbuilt "format.xls" support that comes with Ruby on Rails.
The problem is that, when we get our Excel, Save as Type appears on the webpage. I want to change this to Microsoft Office Excel Workbook (*xls). How to do it?
Below is my controller action code and the respective view with which we get the excel.
Get_excel methods in controller
def get_excel
format.xls do
headers['Content-Type'] = "application/vnd.ms-excel"
headers["Content-disposition"] = 'inline; filename="myexcel.xls"'
headers['Cache-Control'] = ''
end
end
VIEW (get_excel.xls.erb)
<html xmlns:o="urn:schemas-microsoft-com:office:office
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40"
xmlns:ss = "urn:schemas-microsoft-com:office:spreadsheet" />
<head>
<meta http-equiv=Content-Type content="text/html; charset=UTF-8"/>
<meta name=ProgId content=Excel.Sheet/>
<meta name=Generator content="Microsoft Excel 11"/>
<style type="text/css">
#page {
mso-header-data : '&R CONFIDENTIAL';
margin: 0.75in 0.20in 0.5in 0.20in;
mso-header-margin: 0.25in;
mso-footer-margin:0.25in;
mso-page-orientation: landscape;
}
table{
page-break-inside: avoid;
}
td{
white-space:nowrap;
}
</style>
<!--[if gte mso 9]><xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets
<x:ExcelWorksheet>
<x:Name>Gantt Detail</x:Name>
<x:WorksheetOptions>
<x:DefaultRowHeight>319</x:DefaultRowHeight>
<x:Print>
<x:FitHeight>15</x:FitHeight>
<x:ValidPrinterInfo/>
<x:Scale>74</x:Scale>
<x:HorizontalResolution>600</x:HorizontalResolution>
<x:VerticalResolution>600</x:VerticalResolution>
</x:Print>
<x:Selected/>
<x:FrozenNoSplit/>
<x:SplitHorizontal>5</x:SplitHorizontal>
<x:TopRowBottomPane>5</x:TopRowBottomPane>
<x:ActivePane>2</x:ActivePane>
<x:Panes>
<x:Pane>
<x:Number>3</x:Number>
</x:Pane>
<x:Pane>
<x:Number>2</x:Number>
</x:Pane>
<x:ProtectContents>False</x:ProtectContents>
<x:ProtectObjects>False</x:ProtectObjects>
<x:ProtectScenarios>False</x:ProtectScenarios>
</x:WorksheetOptions>
</x:ExcelWorksheet>
</x:ExcelWorksheets>
<x:WindowHeight>8580</x:WindowHeight>
<x:WindowWidth>12120</x:WindowWidth>
<x:WindowTopX>120</x:WindowTopX>
<x:WindowTopY>45</x:WindowTopY>
<x:ProtectStructure>False</x:ProtectStructure>
<x:ProtectWindows>False</x:ProtectWindows>
</x:ExcelWorkbook>
<x:ExcelName>
<x:Name>Print_Titles</x:Name>
<x:SheetIndex>1</x:SheetIndex>
<x:Formula>='Gantt Detail'!$3:$5</x:Formula>
</x:ExcelName>
</xml><![endif]-->
</head>
<body>
<table>
<tr>some code</tr>
<tr>some code</tr>
<tr>some code</tr>
<tr>some code</tr>
<tr>some code</tr>
</table>
</body>
In config/initializers/mime_types.rb
Mime::Type.register "application/vnd.ms-excel", :xls
Then you shouldn't need to set headers directly in the method.
I'm not sure if this will stop you from being prompted, but it's how I serve HTML tables as xls and nobody ever has a problem opening them.
I also don't set a filename, as the method name will name it (get_excel.xls in your case).
I also don't set all the xml and xmlns stuff either. It just works.

Resources