2

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

2
  • 1
    Can you share with us what your script must do? Commented Feb 26, 2015 at 20:21
  • The script is long!!! but at the end it will launch an EC2 instance! Dost this much enough or you need more info? Commented Feb 26, 2015 at 20:29

2 Answers 2

1

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.

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

Comments

0

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.

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.