alilland alilland - 5 months ago 53
Ruby Question

How to run external Ruby scripts from a Sinatra application

I have a Ruby script in a folder outside of my Sinatra application that I would like to run with a button click.

This is app.rb file:

require 'sinatra'

get '/' do
erb :home
end

get '/launch_script' do
system("ruby path\\to\\file\\delete_rows_csv.rb")
end


This is home.erb file:

<a href='/launch_script'> Launch a Script </a>


Am I supposed to load a filepath or require the file to get this to work?




This is the solution that worked great:

require 'sinatra'

get '/' do
erb :home
end

get '/launch_script' do
load 'path\\to\\file\\delete_rows_csv.rb'
end

Answer

You can use require or require_relative (depending if your file is located in a static place or relative to this file). Adding .rb is not required at the end either.

require 'sinatra'

get '/' do
  erb :home
end

get '/launch_script' do
  require 'path\\to\\file\\delete_rows_csv'
end