1

I would like to know how I can run a file.rb directly into controller. For example, I have followed the tuto for Twilio, and it's working using the command : "ruby test.rb" into terminal to run the file test.rb into the app folder.

But I don't know how I can run this file directly into my controller. For example :

TestController.rb 
 def test_sms
  run "test.rb"
 end
end

UPDATE

According to @Seth answer, the good solution looks like that.

My file :

class Test
  attr_accessor :body

  def initialize
    # ...
  end

  def do_something
    self.body = "Test string params"
  end
end

And my controller :

require "path/to/file"
class TestController < ApplicationController
  def test_sms
    test = Test.new
    test.do_something
    body = test.body
    print body
  end
end
jacques Mesnet
  • 607
  • 3
  • 10

2 Answers2

2

Given a test file such as the following:

# your file
class Test
  def initialize
    # ...
  end

  def do_something
    # ...
  end
end

All you need to do is require said file and then invoke whatever it is you need to:

# your controller
require "path/to/file"
class TestController < ApplicationController
  def test_sms
    test = Test.new
    test.do_something
  end
end
Seth
  • 10,198
  • 10
  • 45
  • 68
  • It's exactly what I'm looking for, but I have a problem to pass params generated on my file to the controller For example: The line " print result.body " is working, so I've try to create a params " @body = result.body " but I cannot acces it into my controller. Any Idea ? – jacques Mesnet Nov 27 '19 at 15:29
  • @jacquesMesnet I'd have to see the code to understand more, but it sounds like you need to define `attr_reader :body` within the class so that the instance variable can be read within the scope of its caller. – Seth Nov 27 '19 at 15:39
  • 1
    I have remade a answer with the code that I'm trying to run bellow – jacques Mesnet Nov 27 '19 at 15:56
0

According to @Seth answer, the good solution looks like that. But I cannot access to the params @body created on the file.rb through the controller. Any idea ?

My file :

class Test
  def initialize
    # ...
  end

  def do_something
    @body = "Test string params"
  end
end

And my controller :

require "path/to/file"
class TestController < ApplicationController
  def test_sms
    test = Test.new
    test.do_something
    print @body
  end
end
jacques Mesnet
  • 607
  • 3
  • 10
  • Instead adding additional information through an answer or comment you should [edit](https://stackoverflow.com/posts/59072982/edit) your original question. – 3limin4t0r Nov 27 '19 at 16:17
  • `@body` in `TestController` is not the same as `@body` in `Test`. You should add an attribute reader to `Test` to read from the attribute. `attr_reader :body` just under `class Test`. Then in the controller use `print test.body`. – 3limin4t0r Nov 27 '19 at 16:20
  • Ok thanks for your answer, I've edit my post as you suggest to be cleaner. I test your solution right now, but I'm not really sure to understand it. I'm gonna look what is attr_reader on gg – jacques Mesnet Nov 27 '19 at 16:24
  • 1
    See: https://stackoverflow.com/questions/5046831/why-use-rubys-attr-accessor-attr-reader-and-attr-writer – 3limin4t0r Nov 27 '19 at 16:32
  • This was more usefull to explain my particular case : https://stackoverflow.com/questions/10133172/rails-attr-accessor-undefined-method-in-the-setter – jacques Mesnet Nov 27 '19 at 16:46
  • Thanks @3limin4t0r !! – jacques Mesnet Nov 27 '19 at 16:46