PDA

View Full Version : Execute/Run the Ruby (.rb) files from browser.


rajug
December 1st, 2006, 12:00 AM
Hello ruby users,

I am not new at all for ruby because i have studied for a month about ruby. Actually i need to create a heatmap (the overlay image that shows the click locations in my web page) for my site using ruby on rails which i got reference from http://blog.corunet.com/english/the-definitive-heatmap. And i am successfull to create heatmap in my local machine. Also i tried everything installing (ImageMagick, RMagick, Ruby as well as other needed software) and i am success now to create the images using RMagick/ImageMagick in the server from COMMAND LINE only. When i run the image creating code form command line as ruby something.rb. It reads the data from a text file and writes into a file that shows the click maps of my site. But when i run this same code from browser from a controller class and method, it does not work neither it gives me the error.

And hereby now i want to know how to run or how to call the something.rb file which generates the image successfully from command line (SSH in PUTTY) from my controller class (calling method). How can we execute that .rb file from my ruby code (executing ruby files).

Someone suggest me to do with ` (backtick) in a forum like this :

`ruby /path/to/myfile/toexecute/file.rb`
class HeatmapController < ApplicationController
layout "standard-layout"
def initialize
require 'rubygems'
require 'RMagick'
end
def index
end
def write_click
#something to write into file
end
def WriteImage
`ruby /path/to/myfile/toexecute/file.rb`
end
end


Then when i tried http://www.mydomain.com/rails/heatmap/WriteImage it does not work.

I dont know whether i could make you understood my problem. Please help me. I am spending lots of days for this but i could not do.

Thank you very much help in advance.

Raju Gautam
A New Ruby Programmer

rob
December 1st, 2006, 07:24 PM
The method defintion (def WriteImage) is certainly right, as the backtick is the way to call external programs, but you don't need to also call it here:

ruby /path/to/myfile/toexecute/file.rb`
class HeatmapController < ApplicationController

Also, what errors are you getting in your logs and/or in the browser?

rajug
December 2nd, 2006, 11:26 PM
The method defintion (def WriteImage) is certainly right, as the backtick is the way to call external programs, but you don't need to also call it here:

ruby /path/to/myfile/toexecute/file.rb`
class HeatmapController < ApplicationController

Also, what errors are you getting in your logs and/or in the browser?

Thank you very much for the response. What do you mean by "you dont need to also call it here.."? Please make me little bit clear about this if you dont mind. Do you mean i dont have to call it within a controller? if so how can i call this? please give me some tips about calling this method or to execute this file.

And it does not really give any error too. It just runs the file and show the .rhtml content in the browser.

Thanks in advance.

With Regards
Rajug

rob
December 3rd, 2006, 01:34 AM
Oh, I see what you mean.

Turn this:

`ruby /path/to/myfile/toexecute/file.rb`
class HeatmapController < ApplicationController
layout "standard-layout"
def initialize
require 'rubygems'
require 'RMagick'
end
def index
end
def write_click
#something to write into file
end
def WriteImage
`ruby /path/to/myfile/toexecute/file.rb`
end
end

Into this:

class HeatmapController < ApplicationController
layout "standard-layout"
def initialize
require 'rubygems'
require 'RMagick'
end
def index
end
def write_click
#something to write into file
end
def WriteImage
`ruby /path/to/myfile/toexecute/file.rb`
end
end

Then, use render_text, like this:

def WriteImage
render :text => `ruby /path/to/myfile/toexecute/file.rb`
end

rajug
December 3rd, 2006, 02:28 AM
Oh, I see what you mean.

Turn this:

`ruby /path/to/myfile/toexecute/file.rb`
class HeatmapController < ApplicationController
layout "standard-layout"
def initialize
require 'rubygems'
require 'RMagick'
end
def index
end
def write_click
#something to write into file
end
def WriteImage
`ruby /path/to/myfile/toexecute/file.rb`
end
end

Into this:

class HeatmapController < ApplicationController
layout "standard-layout"
def initialize
require 'rubygems'
require 'RMagick'
end
def index
end
def write_click
#something to write into file
end
def WriteImage
`ruby /path/to/myfile/toexecute/file.rb`
end
end

Then, use render_text, like this:

def WriteImage
render :text => `ruby /path/to/myfile/toexecute/file.rb`
end

Ok dear rob,

Do i have to write this method twice? If yes then where to write this inside the controller class or outside?
Actually why do i need to render_text? I think i dont have to do that because i dont need any output from that file. This file just creates the images in some folder.

And i am confused on path too. Does the ruby take the relative path or absolute path? I have tried with both relative as well as absolute path. But the image creating is not working from browser. But i can do this creating from command line as i have mentioned above in main post.

Thanx.

With Regards
Raju