I have a ruby script (example.rb) and I want to run it when a user click on a button on my Ruby on Rails website! What are the steps in order to do that?
Thanks
I have a ruby script (example.rb) and I want to run it when a user click on a button on my Ruby on Rails website! What are the steps in order to do that?
Thanks
First you need to have an action that will be called upon clicking the button. Then in the action you can use %x to call your script. For example:
def my_action
result = %x(ruby example.rb)
end
If you want to run the script in the context of the rails application, you will want to use rails runner.
def my_action
result = %x(bin/rails runner example.rb)
end
You can find more useful info about executing shell commands in ruby here.
This question is old but still comes up on the first page of a google search so here is what I use.
The rails runner command will execute the file in the context of your rails app.
rails runner [path to file].rb
Edit: After using this a few times, I found that you can add RAILS_ENV=production to the beginning of the command to specify the environment to run the script in.