# CGI Script Inside Rails?

**URL:** <https://www.ruby-forum.com/t/cgi-script-inside-rails/137574>\
**Category:** Rails\
**Created:** [May 14, 2008, 9:36am UTC](https://www.ruby-forum.com/t/cgi-script-inside-rails/137574 "2008-05-14T09:36:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Merv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@Merv](https://www.ruby-forum.com/u/Merv)\
**Post date:** [May 14, 2008, 9:36am UTC](https://www.ruby-forum.com/t/cgi-script-inside-rails/137574/1 "2008-05-14T09:36:18Z")

</div>

In the depot example we use a “Empty cart” button to empty the cart.  
However, Instead of clicking the “Empty Cart” button, we want to call  
a script from inside a .SWF(Flash) object to do it.

How do we write a .rb script to do something in the Rails  
environment?

-Frank

# ~/rails/depot/app/controllers/store\_controller.rb

class StoreController \< ApplicationController  
before\_filter :find\_cart, :except =\> :empty\_cart

```
def index

```

@products = Product.find\_products\_for\_sale  
@cart = find\_cart  
end

```
def add_to_cart

```

begin  
product = Product.find(params[:id])  
rescue ActiveRecord::RecordNotFound  
logger.error(“Attempt to access invalid product #{params[:id]}”)  
redirect\_to\_index(“Invalid product”)  
else  
@cart = find\_cart  
@current\_item = @cart.add\_product(product)  
redirect\_to\_index unless request.xhr?  
end  
end

```
def empty_cart

```

# session[:cart] = nil redirect\_to\_index end end

---

<div class="post-metadata">

**Author:** ![Merv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@Merv](https://www.ruby-forum.com/u/Merv)\
**Post date:** [May 14, 2008, 11:18am UTC](https://www.ruby-forum.com/t/cgi-script-inside-rails/137574/2 "2008-05-14T11:18:45Z")

</div>

On 14 May 2008, at 06:29, Merv wrote:

> In the depot example we use a “Empty cart” button to empty the cart.  
> However, Instead of clicking the “Empty Cart” button, we want to call  
> a script from inside a .SWF(Flash) object to do it.
> 
> How do we write a .rb script to do something in the Rails  
> environment?

Why can’t your flash object make the same http request that clicking  
on the button would have done ?

Fred
