# Executing .rb file within view

**URL:** <https://www.ruby-forum.com/t/executing-rb-file-within-view/119250>\
**Category:** Rails\
**Created:** [November 5, 2007, 12:42pm UTC](https://www.ruby-forum.com/t/executing-rb-file-within-view/119250 "2007-11-05T12:42:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 5, 2007, 12:42pm UTC](https://www.ruby-forum.com/t/executing-rb-file-within-view/119250/1 "2007-11-05T12:42:07Z")

</div>

Hello everyone,

I need to execute a ruby script from within my view. It’s a script  
that dynamically generates an image as output, so the invocation  
should look something like this:  
\<%= image\_tag “/images/myScript.rb?args=foobar”%\>

Now I don’t know how to configure WEBrick to execut the ruby file when  
I put it in my project folder in public/images and access it through  
localhost:3000/project/images/myScript.rb  
When doing so, my browser displays the source of myScript.rb instead  
of executing it.

Another possibility would certainly be to run my script as a webserver  
on a different port and invoking it like this:  
\<%= image\_tag “localhost:3001/myScript.rb?args=foobar”%\>  
but a second server running seems to be a little bit of overkill to  
me.

I’d apreciate any suggestions. If there is any better way to do this,  
please let me know.

Thanks a lot,  
Frank

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 5, 2007, 12:47pm UTC](https://www.ruby-forum.com/t/executing-rb-file-within-view/119250/2 "2007-11-05T12:47:23Z")

</div>

On 11/5/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> localhost:3000/project/images/myScript.rb  
> When doing so, my browser displays the source of myScript.rb instead  
> of executing it.
> 
> Another possibility would certainly be to run my script as a webserver  
> on a different port and invoking it like this:  
> \<%= image\_tag “localhost:3001/myScript.rb?args=foobar”%\>  
> but a second server running seems to be a little bit of overkill to  
> me.

You could use rails and call your script from a controller then render  
it :  
render :text =\> result\_from\_script  
or  
render :inline =\> result\_from\_script

–  
Fabien J.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 5, 2007, 1:07pm UTC](https://www.ruby-forum.com/t/executing-rb-file-within-view/119250/3 "2007-11-05T13:07:19Z")

</div>

thanks for the quick reply.  
this approach may insert some external HTML or whatever into my final  
output to the browser, but I cannot embed the image like this, right?  
The idea is to invoke the script from the client and produce a HTML  
response followed by the image data. The reason for this is because  
the image is not saved as a file (for performance), so I have to  
directly send it to the client after computation.

frank

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 5, 2007, 1:21pm UTC](https://www.ruby-forum.com/t/executing-rb-file-within-view/119250/4 "2007-11-05T13:21:20Z")

</div>

I have just discovered the “send\_data” method which seems to do  
exactly what I need: send a string containing binary data to the  
client. It even lets me specify the content type, so it’s perfect for  
my purpose.  
I’ll try it out and let you know if it worked out.

Frank

On Nov 5, 1:06 pm, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 5, 2007, 7:26pm UTC](https://www.ruby-forum.com/t/executing-rb-file-within-view/119250/5 "2007-11-05T19:26:37Z")

</div>

it worked out pefectly. I created an additional controller that solely  
returns the image using send\_data() instead of rendering any template.

thanks for your help.  
frank

On Nov 5, 1:20 pm, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
