phantomJS rails controller localhost not working - ruby-on-rails

I'm having a strange problem with PhantomJS and capturing a screenshot when the code is called from inside my rails controller AND it's opening a page from the same application - ie. localhost:3000/my/page. Outside of the rails controller (rails console or directly from terminal, it works fine). External URLs such as google.com, amazon.com work fine too.
What I can see:
phantomjs stops after page.onLoadStarted and never goes beyond that.
What I've tried:
Different servers:
Webrick, Puma and Unicorn and all have the same problem.
Different IPs:
I've changed the url inside the script.js file to load localhost, an internal IP and an external IP that connects to rails app. They all fail.
script.js
var page = require('webpage').create();
page.open('http://localhost:3000/my/page');
page.onLoadFinished = function() {
page.render('image.png');
phantom.exit();
}
I don't think the problem is with script.js because if I run phantomjs script.js in terminal, it works.
Is it not possible to get a screenshot from the same server that is initializing phantomjs, even if it's a different page/controller?

Related

How to automatically launch browser when rails server starts?

Is there a way of automatically launching your browser and pointing it to http://localhost:3000 when you run rails server?
And secondly ensuring this only happens in development?
I would have thought that Launchy would have done the trick, but I'm struggling to work out where to put it.
I've tried adding an initialiser config/initializers/launchy.rb
require 'launchy'
Launchy.open("http://localhost:3000")
And this triggers the browser to open, create a new tab and visit http://localhost:3000 as expected, however it runs before the server / application has finished booting and I get a Cannot connect to Server error in the browser.
If I reload in the browser my app works just fine so I'm confident it's not a problem with my app, rather the timing of the Launch.open call.
I tried renaming launchy.rb to z_launchy.rb so it gets loaded last as per the docs but still the same problem. Launchy fires before the Application is ready for it.
I've also tried adding the code to config/puma.rb (I'm using puma as my server), to config/enviroments/developoment.rb but always the same problem. The Launchy.open command gets called to soon.
Where should I call Launchy.open("http://localhost:3000") to ensure that a) It runs after the application has loaded and is ready to receive requests; and b) So it only runs in development, not in test or production?
System set up OS X 10.11.3, Rails 4.2.5, ruby 2.2.1p85, puma version 2.15.3
Putting it into config.ru would work, after the run Rails.application line. By the time that returns, your app is ready to go. To run it only in development, check the Rails.env.
require ::File.expand_path('../config/environment', __FILE__)
run Rails.application
Launchy.open("http://localhost:3000") if Rails.env.development?
This is easy, but it feels dirty to me to have this sitting in your app. Better would be a small script that starts your server, waits a moment, then launches the browser.

Favicon showing up in multiple Rails apps

I'm having an unusual problem - I uploaded a favicon into one of my rails applications into the application.html.erb file into the head using this code:
<%= favicon_link_tag 'favicon.ico' %>. This works fine, the favicon shows up, but when I close that application and open a new one into my localhost:3000, the same favicon is showing up, but there's no code in that application for it, in the head nor in the public/favicon file. Any idea how this can be happening?
That's probably because of the cached version on browser for address localhost:3000 i.e. 127.0.0.1:3000. Try changing the port number to something else. For example: localhost:3001 by running this command:
rails s -p 3001
You won't see the same favicon now.

No log messages in production.log

I wrote a demo HelloWorld Rails app and tested it with WEBrick (it doesn't even use a DB, it's just a controller which prints "hello world"). Then I tried to deploy it to a local Apache powered with Passenger. In fact this test is just to get Passenger working (it's my first deploy on Apache). Now I'm not even sure that Passenger works, but I don't get any error on the Apache side.
When I fire http://rails.test/ the browser shows the Rails 500 error page - so I assume that Passenger works. I want to investigate the logs, but it happens that production.log is empty! I don't think it's a permission problem, because if I delete the file, it is recreated when I reload the page. I tried to change the log level in conf/environments/production.rb, tried to manually write to log file with Rails console production and
Rails.logger.error('asdf')
it returns true but nothing gets written to production.log. The path (obtained per Rails.logger.inspect) is correct, and I remark that the file is recreated if I manually remove it. How can I know what's going on?
(I already checked the Apache logs, plus I set the highest debug level for Passenger but it seems a Rails problem, so is not logged by the server)
Assuming you're running Rails 3.2.1, this is a bug. It was patched in 3.2.2.
If you can't upgrade to 3.2.2 for any reason, this comment on GitHub has a workaround:
# config/initializers/patch_rails_production_logging.rb
Rails.logger.instance_variable_get(:#logger).instance_variable_get(:#log_dest).sync = true if Rails.logger
Setting this works on Rails 3.2.11:
Rails.logger = ActiveSupport::BufferedLogger.new(Rails.root.join("log","production.log"))

thin slow in development when not using localhost

I switched to using thin in local development instead of webrick.
When I access localhost:3000 it returns the page almost instantly just like webrick
But when I access myapp.local:3000 the browser spins for 20 seconds or so on each request before rendering the page. I'm not sure what it's doing during that time - the rails log shows the page being generated almost instantly - it almost seems like the browser is doing name resolution during that time or something else.
In my /etc/hosts i have
127.0.0.1 myapp.local
In webrick there was no difference between accessing myapp.local:3000 and localhost:3000.
But in thin there is the large difference mentioned above. Any theories? Much appreciated!
Look for the file /usr/lib/ruby/VERSION_OF_RUBY/webrick/config.rb and edit it.
Replace/insert the following line as a new key of the General hash.
:DoNotReverseLookup => true
Restart webrick.
Otherwise try running sudo service avahi-daemon stop
See Webrick is very slow to respond. How to speed it up? for more details

Why would running a Rails app as a WEBrick server work, but installing it as a Mongrel service would not?

Yet another newbie RoR question from me.
I started banging my head against a wall last night when I simply could not get my Rails app to display in my browser after installing it as a Mongrel service.
I installed it using a command like this (from the app's root directory):
mongrel_rails service::install -N MyAppName -e development -p 3000
This set up the Windows service and everything seemed to be just fine. I could start/stop the service and saw no errors in the logs. Then navigating to localhost:3000 in my browser, I was greeted with a variety of errors, none Rails-specific (all along the lines of "Could not connect to server" or the like). Consulting the log at this point revealed no obvious problems.
I could not for the life of me figure out how to get this to work. So, out of exasperation, I tried simply running the app on WEBrick instead:
ruby script/server webrick -p 3000
When I did this, my app ran perfectly! Opening my browser to localhost:3000 now displayed my front page as expected.
I should note that I have used Mongrel successfully for other apps on my local machine.
So what app-specific characteristics could be responsible for WEBrick working where Mongrel doesn't?
Just some ideas to try:
Add -c param with full path to application:
-c "C:\xxx\yyy\zzz"
Check if system-wide PATH environment variable contains ruby bin directory - maybe just user's PATH is set.
Switch service to run as your user.

Resources