# How To: Build shell equivalent of a rails app

**URL:** <https://www.ruby-forum.com/t/how-to-build-shell-equivalent-of-a-rails-app/119271>\
**Category:** Rails\
**Created:** [November 5, 2007, 6:11pm UTC](https://www.ruby-forum.com/t/how-to-build-shell-equivalent-of-a-rails-app/119271 "2007-11-05T18:11:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [November 5, 2007, 6:11pm UTC](https://www.ruby-forum.com/t/how-to-build-shell-equivalent-of-a-rails-app/119271/1 "2007-11-05T18:11:39Z")

</div>

Hey folks, I have an application that I have written in Rails and I am  
wondering about the feasability of writing an equivalent as a shell  
app using rails concepts. The initial app was interactive and made  
sense beign implemented for the web, but this new app needs to be run  
from the command line with its output dumped to stdout.

I have played around with ActiveRecord and understand how to use it in  
a ruby shell app, but I am not sure how I might be able to get  
ActionView to work or similiar behavior to allow a controller and view  
to be implemented as in a rails app.

Does anyone know of an example I can look at which does the above, or  
if not does anyone have suggestions for where I can look for answers?

Thanks in advance for any help you can offer!  
-Andy

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [November 5, 2007, 7:03pm UTC](https://www.ruby-forum.com/t/how-to-build-shell-equivalent-of-a-rails-app/119271/2 "2007-11-05T19:03:26Z")

</div>

Andrew C. wrote:

> Hey folks, I have an application that I have written in Rails and I am  
> wondering about the feasability of writing an equivalent as a shell  
> app using rails concepts. The initial app was interactive and made  
> sense beign implemented for the web, but this new app needs to be run  
> from the command line with its output dumped to stdout.
> 
> I have played around with ActiveRecord and understand how to use it in  
> a ruby shell app, but I am not sure how I might be able to get  
> ActionView to work or similiar behavior to allow a controller and view  
> to be implemented as in a rails app.
> 
> Does anyone know of an example I can look at which does the above, or  
> if not does anyone have suggestions for where I can look for answers?

ruby script/console will drop you into an IRB session with all your  
models loaded. From there you can require an interactive script you’ve  
written.

HTH, Daniel

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [November 5, 2007, 7:31pm UTC](https://www.ruby-forum.com/t/how-to-build-shell-equivalent-of-a-rails-app/119271/3 "2007-11-05T19:31:19Z")

</div>

Hey Andy,

I’ve got a little script I call controller\_instance you can have if  
you wish, it can be used like so:

script/controller\_instance RssController google\_base\_feed -e  
production

This spits out the response from the google\_base\_feed action of the  
Rss controller (if that wasn’t obvious:-)

I use it in several cron jobs… something like this

open(rss\_file, ‘w’) do | f |  
cmd = open("|"+rootd +“script/controller\_instance RssController  
google\_base\_feed -e #{ENV[‘RAILS\_ENV’] == ‘production’ ?  
‘production’ : ‘development’}”)  
while string = cmd.gets  
f.write(string)  
end  
cmd.close  
end

This avoids having to use a tool such as curl which in my opinion is a  
convoluted way to hit your own local server.

I dug this up after \>much\< looking on google. I didn’t write it and  
can’t recall who did but I emailed and thanked him when I found it and  
he was happy to share.

If you would like it please email me at tmac at easystreet dot com.

Tim

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [November 5, 2007, 8:56pm UTC](https://www.ruby-forum.com/t/how-to-build-shell-equivalent-of-a-rails-app/119271/4 "2007-11-05T20:56:46Z")

</div>

Thanks guys, looks like some promising options!

-Andy

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [November 6, 2007, 3:21am UTC](https://www.ruby-forum.com/t/how-to-build-shell-equivalent-of-a-rails-app/119271/5 "2007-11-06T03:21:02Z")

</div>

Hey again, as my luck goes I am having problems with both of the  
suggested routes and am wondering if  
you guys might have any further suggestions for what needs to be  
tweaked.

1. Tim, with the controller\_instance script you sent me I see this  
problem when I try to use it with a basic rails app that I created  
just to test with it, has a model tied to a database table and just  
iterates through some of the records and displays two of the fields  
( in the web app I see it working fine ).  
When I try this:

ruby script/controller\_instance MainController index -e development

I see this error:

undefined method `camelize’ for “top”:String

I googled the error and the only proposed solutions I could find were  
not applicable to my situation so am unsure what might be causing  
this.

1. Daniel, I also tried script/console and it immediately generates  
the following error:

Loading development environment.  
/usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/console.rb:  
25:in `exec': No such file or directory - irb -r irb/completion -r script/../config/../config/environment -r console_app -r console_with_helpers --simple-prompt (Errno::ENOENT) from /usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/ commands/console.rb:25 from /usr/local/lib/ruby/site_ruby/1.8/rubygems/ custom_require.rb:27:in`require’  
from script/console:3

Do I have an inferior rails installation? 😉 I have a gem-installed  
rails version 1.2.3

Anyway, wondering if any of this rings a bell, maybe there is  
something easy to tweak to fix it.

Thanks again folks!

-Andy

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [November 26, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/how-to-build-shell-equivalent-of-a-rails-app/119271/6 "2007-11-26T23:04:17Z")

</div>

Hey Andy,

Sorry I didn’t notice you posted this question/issue, I just happened  
across it while looking for something else.

I suspect you’ve already found a solution but anyway… as far as the  
script issue …you could probably do something like:

require ‘active\_support/core\_ext/string/inflections’  
include ActiveSupport::CoreExtensions::String::Inflections

or perhaps

require ‘active\_support/inflector’  
include ActiveSupport::Inflector

or something similar.

Should give the script access to the camelize method.

best!  
Tim
