Rails Server Route Error When Accessing Via IP Address - ruby-on-rails

I am new here and working on a Rails application that was handed to me for a research project.
I have created my own instance of the project and I am able to access the server running the instance from my computer, BUT, only if I modify my hosts file and map the IP address to an arbitrary domain name.
If I try to access the rails server via an IP address, I get the following error:
"Routing Error
No route matches [GET] "/"
Try running rake routes for more information on available routes."
So my question is, how would I configure the Rails server and application to be accessible via IP address only, for example: http://52.78.233.65:3000/, and why would the server work if I modify the hosts file but not visit the site directly?

If you need remote access, you can choose one of below methods:
Apply in each start
Start rails server by appending -b 0.0.0.0, so command to start rails server is: rails s -b 52.78.233.65 (from your sample IP)
Change default binding option
Modify config/boot.rb by adding below code and start rails server with normal command rails s
config/boot.rb
require 'rubygems'
require 'rails/commands/server'
module Rails
class Server
alias :default_options_bk :default_options
def default_options
default_options_bk.merge!(Host: '0.0.0.0')
end
end
end

Related

Rails: How to run code when server starts up, but not when running a rake task or the console?

Before, I added code to a file called config/initializers/remote_publishers.rb which set up a connection to RabbitMQ using the Bunny gem on server startup.
However, this is now also executed when running rails c, rails g model SomeModel foo:integer, rails db:migrate etc.
For this app, the RabbitMQ-connection only makes sense when rails is started using rails s(erver).
What is the proper way to conditionally execute this code? Is there a way to see if Rails is starting as server, or only as task-runner?
What web server are you using? On Puma, for example, you can use
on_worker_boot do
# Establish RabbitMQ connection
end
Another possibility might be to check if defined?(Rails::Server) in your initializer: this should only be true when running in the context of the web server.

Read Local file from Remote Location via SSH in Ruby

I want to access a local file which is located on a device via a remote server location via SSH.
The local file is in this directory
/Desktop/applications.csv
and the IP address of the machine it is located in is 192.168.1.1 and user is user1
How do i read it from a remote location using SSH in Ruby on Rails?
I tried doing this but it fails
hash = Digest::MD5.hexdigest(File.read("ssh user#192.168.1.1 /Desktop/applications.csv"))
Not sure how to go about it. Any help appreciated.
Have you looked into SSHKit? It lets you run commands on remote machines, via ssh.
As an example:
require 'sshkit'
require 'sshkit/dsl'
include SSHKit::DSL
on '192.168.1.1'
within "/Desktop/" do
with rails_env: :production do
rake "read_applications"
end
end
end
And the 'read_applications' rake task would do the File.read and whatever else was required.
EDIT:
Does it know what user you're authenticating with? Might need to add the user like this:
on '192.168.1.1'
within "/Desktop/" do
as :user do
# do something
end
end
end
And change :user to be your username. There's loads more examples on the Github page.

Rspec Capybara get port before run first visit method

I'm try to run Capybara server before all tests to get server port which I should include in url like 'http://subdomain.domain.au:CURRENT_PORT_NUMBER/'
so
# I create new session:
session = Capybara::Session.new(:selenium)
# then I start browser
session.driver.browser
# And try to get port number like some folks suggested
Capybara.current_session.driver.rack_server.port # => nil
# And get nil
In my opinion when browser started you should have port number somewere. Or I'm wrong?
I do not want hardcode port number for all requests.
Can anyboady suggest how can I get port to visit my application.
As far as I understand browser it's another process and can be started without local server. Thats make sence only when you query server over network. There should be mechanism to run rack server when 'selenium' test start. Isn't it?
You can use Capybara.default_port to get/set the port.

Cramp and heroku

I have been playing around with Cramp to do some real time pushing of information in an app. Its all working great locally but when I push off to heroku I seem to be having issues with the ports.
I have a socket set up in cramp which inherits from websocket
class LiveSocket < Cramp::Websocket
and I also have a cramp action called home which basically just renders some erb for the home page
class HomeAction < Cramp::Action
in my route file I set up the following and also a static file server
Rack::Builder.new do
puts "public file at #{File.join(File.dirname(__FILE__), '../public')}"
file_server = Rack::File.new(File.join(File.dirname(__FILE__), 'public'))
routes = HttpRouter.new do
add('/').to(HomeAction)
get('/LiveSocket').to(LiveSocket)
end
run Rack::Cascade.new([file_server, routes])
end
Then on the client end the javascript connects to
var ws = new WebSocket("ws://<%= request.host_with_port %>/LiveSocket");
As I say locally it all works. We connect and start receiving notifications from the server. On heroku we run thin on the Cedar stack and have a profile which looks like
web: bundle exec thin --timeout 0 start -p $PORT
When I load up the site the page itself loads fine but on trying to connect the websocket I get an error which says
servername.herokuapp.com Unexpected response code: 200
I am guessing this has something to do with how heroku routes its requests but I do know that you can run a node.js websocket server on heroku so figure there must be a way to get this working too.
Thanks in advance for any help.
cheers
stuart
I don't think Heroku supports websockets :( http://devcenter.heroku.com/articles/http-routing#the_herokuappcom_http_stack

How to tell which port Rails is running on in an initializer

Is there a way to tell which port a Rails application (or a generic Rack app) is running on
in an initializer ?
I would like to be able to load a different configuration based on the port or the host name, in order to connect to a host-specific FaceBook application.
I'm using Rails 2.3.5.
This is not very clean but this is a way you can get the port you declared when calling :
rails server -p portnumber
wherever you want in your application (for rails 3).
Here is my scripts/rails.rb file :
#!/usr/bin/env ruby
# This command will automatically be run when you run "rails"
# with Rails 3 gems installed from the root of your application.
ENV['PORT']=ARGV[3]
APP_PATH = File.expand_path('../../config/application', __FILE__)
require File.expand_path('../../config/boot', __FILE__)
require 'rails/commands'
Then whenever you want to get the port number of your server, all you need to do is call ENV['PORT'].
You can call Rails::Server.new.options[:Port] to get the port that your Rails server is running on. This will parse the -p arg from your rails server command, or default to port 3000.
Based on the lack of answers here and this thread on rubyforum: http://www.ruby-forum.com/topic/196017#new , I think that
there probably isn't a standard way to tell the port.

Resources