Run 'script/generate controller' from inside app

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

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

:wink:

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.

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

Thank you,

Frank

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
[email protected]