3

I made a script in ruby ​​and now I need to run it in a rails application. I'm a little lost, but I have a controller, and wanted through a GET /service /:id returns the result of this script. How can I do this?

4
  • 4
    Can't you make a class out of it, then load it as a part of environment and use? Commented May 6, 2012 at 16:26
  • If you don't want to do @SergioTulentsev solution you can always run the script using exec mentalized.net/journal/2010/03/08/… (but his solution is better) Commented May 6, 2012 at 16:37
  • I see that I have little basis in rails, the best would be to create a method in the helper and then run it correct? EDIT: I just saw that helpers only works for views! And for controllers? Commented May 6, 2012 at 16:56
  • @AnthonySewr And for controllers... Look at my answer Commented May 6, 2012 at 18:03

2 Answers 2

4

By the comments, it seems like you want to make this into a method you can call from your controller.

Simple.

Define the method in the corresponding Model for the Controller you're calling it from (or whatever Model you wish to define the method in) and then call it from the Controller.

Say you have a ScriptRunner model, and you want to call this from the show action of some controller.

In your ScriptRunner model, simply do

def runscript(id)
...
end

and in your controller, do

def show
  ScriptRunner.runscript(params[:id])
  @service = Service.find_by_id(params[:id])
end

..or whatever you want it to do.

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

Comments

2

Sergio Tulentsev approach is correct, Just make a model class out of it and call the model method from your controller.

Alternatively which is wrong and hacky you can just call your ruby script directly from the controller

def get
  @results = system "ruby your_ruby_script.rb" 
end

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.