LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   [Ruby on Rails] backticks don't work under rails. (http://www.linuxquestions.org/questions/programming-9/%5Bruby-on-rails%5D-backticks-dont-work-under-rails-753660/)

bartonski 09-08-2009 05:10 PM

[Ruby on Rails] backticks don't work under rails.
 
I'm trying to use backticks to run a perl script from within my rails code. I know that this isn't good style, but I'm trying to mock something up in Rails that I already have written in Perl.

I simplified my code to illustrate the error (calling 'ls /tmp' instead of the perl script).

The controller is in ./app/controllers/missed_controller.rb (named missed_controller for hysterical raisins)

Code:

class MissedController < ApplicationController

  def get_missed
        cmd = "/bin/ls /tmp"
        output = `cmd`

        if( output == nil )
                @missing = "missing is NIL"
        else
                @missing = "The output of '#{cmd}' is [#{output}]"
        end
  end
end

here's the view (from ./app/views/missed/get_missed.html.erb)

Code:

<html>
    <head> <title> Testing backticks in Rails </title> </head>

    <body>
    <h1> <%= @missing %> </h1>

    </body>
</html>

I fire up ./script/server and point my browser to http://localhost:3000/missed/get_missed

Viewing html code, I see:

Code:

<html>
<head> <title> Testing backticks in Rails </title> </head>

<body>
<h1> The output of '/bin/ls /tmp' is [] </h1>

</body>
</html>

I'm expecting the output of '/bin/ls /tmp' to show up in between the brackets, but that's not happening.

If I run the same command in a straight ruby script, it works:

Code:

#! /usr/bin/ruby

puts `ls /tmp`

does in fact give me a directory listing of /tmp.

Here's the system that I'm running:

Debian Etch

> ruby -v
ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]

> rails -v
Rails 2.3.4

I grabbed ruby 1.8.7 from Etch backports, compiled rubygems from source, then installed rails 2.3.4 as using gem install.

As far as I can tell, there's something about rails that isn't allowing me to do this. I'm guessing that there's a security setting somewhere that I need to disable in order to use backticks or popen from within Rails, but I don't know what to turn off.

bartonski 09-08-2009 05:19 PM

Bah. I'm an idiot.

There's an error sitting under script/server:

Code:

/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.4/lib/action_controller/base.rb:385: command not found: cmd
changed the call to `cmd`

Code:

        output = `cmd`
to

Code:

        output = `#{cmd}`
Worked like a charm.

Do I get to thank myself for the solution? ;-)


All times are GMT -5. The time now is 11:05 AM.