1

I have a Ruby script that I run through the Command Line locally. Now I want to run that script through a browser, with a simple HTML button. Is there any way to make a simple HTML page and run the script similar to a JS onclick event? OR will I have to set up a rails environment first?

2
  • Technically you could use CGI but yuck. There are a lot of ways this could be done short of going full Rails; I might consider something lighter-weight. Commented Feb 10, 2015 at 19:16
  • Take a look at Sinatra. Somewhat "lightweight Rails", a lot simpler. Commented Feb 10, 2015 at 19:33

3 Answers 3

2

To run a Ruby script, you need a Ruby interpreter. JavaScript can be run locally by the browser that executes the HTML page, Ruby no.

Therefore, your cannot run the Ruby code directly in the browser. There are several possible solutions.

You can host the Ruby script somewhere, and have the HTML page reference it. If the script is reasonably simple, you can use one of those services that run a Ruby script in a sandbox.

But if it's a Rails app, no way. You need to host it somewhere.

Short answer, you need a server/computer with the Ruby interpreter to execute your script. It can't be executed by the client browser.

Sign up to request clarification or add additional context in comments.

Comments

1

There are a few options here. Depending on your use case the most likely two are:

  1. Convert your Ruby script into JavaScript via Opal so that it can run in the browser.
  2. Set up a simple server with Sinatra and run it on a server somewhere, then call it via AJAX.

Comments

1

There are couple of options available for your use case but I would use Sinatra for this. Here is a very simple example for you.

your_file.rb

require 'sinatra'

get '/' do
  "<a href='/another-file'> Link to another file </a>"
end

get '/another-file' do
  "Today is #{Time.now}"
end

If you do not have sinatra gem installed then please try

gem install sinatra

Once done then you can run your script on your desire port (I'm using 5000)

ruby your_file.rb -p 5000

Now you can browse your simple app at http://localhost:5000. Please checkout sinatra website for complete documentation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.