Search code examples
ruby-on-railsrubyexecute

Running a Ruby Script from Rails Controller


I have a very basic website where a user inputs text to a text field form and submits this text. The text then is supposed to be a parameter for a Ruby script, which will in turn return data based on this supplied text.

My questions:

  1. Can you execute a Ruby script from a Controller?
  2. If yes, what if this Ruby script takes a few minutes until it returns data? Delayed job?
  3. Instead of running a Ruby script, can I import the function from this Ruby script into my controller?

I am still a novice to Rails so I thank you for any suggestions.


Solution

  • Can you execute a Ruby script from a Controller?

    Yes you can, fa the scripts is external, like:

    system('script.rb')
    

    But of course the better way is to embed the script into the Rails app.

    If yes, what if this Ruby script takes a few minutes until it returns data? Delayed job?

    Of course Delayed Job, see here on how to do that.

    Instead of running a Ruby script, can I import the function from this Ruby script into my controller?

    Of course yes, and that way is preferrable, but anyway if that is available to implement.