# Run 'script/generate controller' from inside app

**URL:** <https://www.ruby-forum.com/t/run-script-generate-controller-from-inside-app/182580>\
**Category:** Rails\
**Created:** [January 30, 2010, 11:10am UTC](https://www.ruby-forum.com/t/run-script-generate-controller-from-inside-app/182580 "2010-01-30T11:10:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frank\_in\_Tennessee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dec6dc/32.png) [@Frank\_in\_Tennessee](https://www.ruby-forum.com/u/Frank_in_Tennessee)\
**Post date:** [January 30, 2010, 11:10am UTC](https://www.ruby-forum.com/t/run-script-generate-controller-from-inside-app/182580/1 "2010-01-30T11:10:05Z")

</div>

Is it possible to run generator commands from within a running rails  
application?

I’m experimenting with creating an application that can create ‘views’  
on the fly.

In my ‘pages\_controller.rb’ I have the following code:

```
# Run Ruby script to create all pages defined in

```

‘website\_pages.txt’  
load “/railsprojects/#{Website.find(@page.website\_id).name}/public/  
build\_pages.rb”

In my ‘build\_pages.rb’ I have the following code:

f = File.open(“website\_pages.txt”, “r”)  
f.each\_line do |page|  
ruby script/generate controller page  
end  
file.close

The ‘ruby script/generate controller page’ command fails. Does anyone  
know how this line of code can be changed so that it will work?

Thanks,

Frank

---

<div class="post-metadata">

**Author:** ![Frank\_in\_Tennessee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dec6dc/32.png) [@Frank\_in\_Tennessee](https://www.ruby-forum.com/u/Frank_in_Tennessee)\
**Post date:** [January 30, 2010, 12:43pm UTC](https://www.ruby-forum.com/t/run-script-generate-controller-from-inside-app/182580/2 "2010-01-30T12:43:08Z")

</div>

[http://www.gotripod.com/2007/10/07/tip-running-command-line-shell-commands-in-ruby-using-x/](http://www.gotripod.com/2007/10/07/tip-running-command-line-shell-commands-in-ruby-using-x/)

Try this instead:

f.each\_line do |page|  
%x[script/generate controller #{page}]  
end  
file.close

😉

---

<div class="post-metadata">

**Author:** ![Frank\_in\_Tennessee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dec6dc/32.png) [@Frank\_in\_Tennessee](https://www.ruby-forum.com/u/Frank_in_Tennessee)\
**Post date:** [January 30, 2010, 2:51pm UTC](https://www.ruby-forum.com/t/run-script-generate-controller-from-inside-app/182580/3 "2010-01-30T14:51:54Z")

</div>

You can also invoke the generator by hand:

require ‘rails/generators’  
require ‘generators/rails/controller/controller\_generator’  
Rals::Generators::ControllerGenerator.start %w(page),  
{}, :destination\_root =\> “path/to/tmp/dir”

Where :destination\_root is the place you want to lay the generated  
templates. You could put them in a temp directory and then zip/tar.gz  
the directory’s content and send them.

---

<div class="post-metadata">

**Author:** ![Frank\_in\_Tennessee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dec6dc/32.png) [@Frank\_in\_Tennessee](https://www.ruby-forum.com/u/Frank_in_Tennessee)\
**Post date:** [January 30, 2010, 6:12pm UTC](https://www.ruby-forum.com/t/run-script-generate-controller-from-inside-app/182580/4 "2010-01-30T18:12:02Z")

</div>

I appreciate everyones feedback. I will experiment with all  
suggestions.

Thank you,

Frank

---

<div class="post-metadata">

**Author:** ![Frank\_in\_Tennessee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dec6dc/32.png) [@Frank\_in\_Tennessee](https://www.ruby-forum.com/u/Frank_in_Tennessee)\
**Post date:** [January 30, 2010, 3:21pm UTC](https://www.ruby-forum.com/t/run-script-generate-controller-from-inside-app/182580/5 "2010-01-30T15:21:42Z")

</div>

Frank\_in\_Tennessee wrote:

> Is it possible to run generator commands from within a running rails  
> application?
> 
> I’m experimenting with creating an application that can create ‘views’  
> on the fly.

This sounds like a bad idea, but if you really want to try it, then I  
think that running the generators is the wrong approach. Remember that  
Ruby can programmatically define classes and methods. That’s what you  
should be doing in this case.

> In my ‘pages\_controller.rb’ I have the following code:
> 
> ```
> # Run Ruby script to create all pages defined in
> 
> ```
> 
> ‘website\_pages.txt’  
> load “/railsprojects/#{Website.find(@page.website\_id).name}/public/  
> build\_pages.rb”
> 
> In my ‘build\_pages.rb’ I have the following code:
> 
> f = File.open(“website\_pages.txt”, “r”)  
> f.each\_line do |page|  
> ruby script/generate controller page  
> end  
> file.close

You really shouldn’t be using a separate controller action for each page  
in this case. Instead, have one controller action that takes the page  
name as a parameter.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
