# Generating CRUD interface automagically

**URL:** <https://www.ruby-forum.com/t/generating-crud-interface-automagically/121439>\
**Category:** Rails\
**Created:** [November 27, 2007, 11:45pm UTC](https://www.ruby-forum.com/t/generating-crud-interface-automagically/121439 "2007-11-27T23:45:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [November 27, 2007, 11:45pm UTC](https://www.ruby-forum.com/t/generating-crud-interface-automagically/121439/1 "2007-11-27T23:45:53Z")

</div>

When I browse to [http://localhost:3000/feeds](http://localhost:3000/feeds) I expect rails to  
“automagically” present a CRUD interface because I’ve done this with a  
previous rails (tutorial) project. It’s like I’m missing a rake  
command,  
or a scaffold or migration command. I think that what _presents_ the  
view would be an action? The action uses a controller?

However, I’m not sure and that’s a bit of a shot in the dark.

Instead of the expected CRUD interfact to the table “feeds” I get the  
following error:

Routing Error

no route found to match “/feeds” with {:method=\>:get}

The title of this page is “Action Controller: Exception caught,” which  
indicates that my intuition/vague recollection about this being related  
to an action is on target – but I’m not positive.

Here’s what I’m doing to generate the above error:

thufir@arrakis ~/rubyCode $  
thufir@arrakis ~/rubyCode $  
thufir@arrakis ~/rubyCode $  
thufir@arrakis ~/rubyCode $ mysql --user=root  
Welcome to the MySQL monitor. Commands end with ; or \g.  
Your MySQL connection id is 8  
Server version: 5.0.44-log Gentoo Linux mysql-5.0.44

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.

mysql\>  
mysql\> show databases;  
±-------------------+  
| Database |  
±-------------------+  
| information\_schema |  
| feed\_on\_feeds |  
| mysql |  
| test |  
±-------------------+  
4 rows in set (0.00 sec)

mysql\> create database straw\_development;  
Query OK, 1 row affected (0.00 sec)

# mysql\> quit Bye thufir@arrakis ~/rubyCode $ ruby straw.rb create create app/controllers create app/helpers create app/models create app/views/layouts create config/environments create components create db create doc create lib create lib/tasks create log create public/images create public/javascripts create public/stylesheets create script/performance create script/process create test/fixtures create test/functional create test/integration create test/mocks/development create test/mocks/test create test/unit create vendor create vendor/plugins create tmp/sessions create tmp/sockets create tmp/cache create tmp/pids create Rakefile create README create app/controllers/application.rb create app/helpers/application\_helper.rb create test/test\_helper.rb create config/database.yml create config/routes.rb create public/.htaccess create config/boot.rb create config/environment.rb create config/environments/production.rb create config/environments/development.rb create config/environments/test.rb create script/about create script/breakpointer create script/console create script/destroy create script/generate create script/performance/benchmarker create script/performance/profiler create script/process/reaper create script/process/spawner create script/process/inspector create script/runner create script/server create script/plugin create public/dispatch.rb create public/dispatch.cgi create public/dispatch.fcgi create public/404.html create public/500.html create public/index.html create public/favicon.ico create public/robots.txt create public/images/rails.png create public/javascripts/prototype.js create public/javascripts/effects.js create public/javascripts/dragdrop.js create public/javascripts/controls.js create public/javascripts/application.js create doc/README\_FOR\_APP create log/server.log create log/production.log create log/development.log create log/test.log cd straw create db/migrate create db/migrate/001\_feeds.rb exists db/migrate create db/migrate/002\_categories.rb (in /home/thufir/rubyCode/straw) (in /home/thufir/rubyCode/straw) == Feeds: migrating

# – create\_table(:feeds) → 0.0294s == Feeds: migrated (0.0300s)

# == Categories: migrating

# – create\_table(:categories) → 0.0097s == Categories: migrated (0.0103s)

```
  exists app/controllers/
  exists app/helpers/
  create app/views/straw
  exists app/views/layouts/
  exists test/functional/

```

dependency model  
exists app/models/  
exists test/unit/  
exists test/fixtures/  
create app/models/feed.rb  
create test/unit/feed\_test.rb  
create test/fixtures/feeds.yml  
create app/views/straw/\_form.rhtml  
create app/views/straw/list.rhtml  
create app/views/straw/show.rhtml  
create app/views/straw/new.rhtml  
create app/views/straw/edit.rhtml  
create app/controllers/straw\_controller.rb  
create test/functional/straw\_controller\_test.rb  
create app/helpers/straw\_helper.rb  
create app/views/layouts/straw.rhtml  
create public/stylesheets/scaffold.css  
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@  
[http://localhost:3000/feeds](http://localhost:3000/feeds)  
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@  
=\> Booting WEBrick…  
=\> Rails application started on [http://0.0.0.0:3000](http://0.0.0.0:3000)  
=\> Ctrl-C to shutdown server; call with --help for options  
[2007-11-27 14:34:25] INFO WEBrick 1.3.1  
[2007-11-27 14:34:25] INFO ruby 1.8.6 (2007-09-24) [i686-linux]  
[2007-11-27 14:34:25] INFO WEBrick::HTTPServer#start: pid=8040  
port=3000  
127.0.0.1 - - [27/Nov/2007:14:34:35 PST] “GET /feeds HTTP/1.1” 404 619

- → /feeds  
[2007-11-27 14:34:45] INFO going to shutdown …  
[2007-11-27 14:34:45] INFO WEBrick::HTTPServer#start done.  
thufir@arrakis ~/rubyCode $  
thufir@arrakis ~/rubyCode $ mysql --user=root  
Welcome to the MySQL monitor. Commands end with ; or \g.  
Your MySQL connection id is 12  
Server version: 5.0.44-log Gentoo Linux mysql-5.0.44

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.

mysql\> show tables from straw\_development;  
±----------------------------+  
| Tables\_in\_straw\_development |  
±----------------------------+  
| categories |  
| feeds |  
| schema\_info |  
±----------------------------+  
3 rows in set (0.00 sec)

mysql\> quit  
Bye  
thufir@arrakis ~/rubyCode $  
thufir@arrakis ~/rubyCode $ cat straw.rb  
#kludgy and brittle, but runs without errors on gentoo  
#may or may not work on windows  
#assumes MySQL is installed

# 

# 

# why doesn’t [http://localhost:3000/feeds](http://localhost:3000/feeds) bring up the db?

require ‘fileutils’

FileUtils.rmtree ‘straw’  
system(“rails straw”)

FileUtils.cd(‘straw’, :verbose =\> true)

FileUtils.rm ‘config/database.yml’, :force =\> true

database\_yml = File.open(‘config/database.yml’, ‘w’)

database\_yml.puts "  
development:  
adapter: mysql  
database: straw\_development  
username: root  
password:  
host: localhost  
socket: /var/run/mysqld/mysqld.sock  
test:  
adapter: mysql  
database: straw\_test  
username: root  
password:  
host: localhost  
socket: /var/run/mysqld/mysqld.sock  
production:  
adapter: mysql  
database: straw\_production  
username: root  
password:  
host: localhost  
socket: /var/run/mysqld/mysqld.sock"

database\_yml.close

system(“script/generate migration feeds”)

FileUtils.rm ‘db/migrate/001\_feeds.rb’, :force =\> true

\_001\_feeds = File.open(‘db/migrate/001\_feeds.rb’, ‘w’)

\_001\_feeds.puts "  
class Feeds \< ActiveRecord::Migration

```
    def self.up
            create_table :feeds do |table|
                    table.column :title, :string
                    table.column :location, :string
                    table.column :category_id, :integer
            end
    end

    def self.down
            drop_table :feeds
    end

```

end"

\_001\_feeds.close

##########categories

system(“script/generate migration categories”)

FileUtils.rm ‘db/migrate/002\_categories.rb’, :force =\> true

\_002\_categories = File.open(‘db/migrate/002\_categories.rb’, ‘w’)

\_002\_categories.puts "  
class Categories \< ActiveRecord::Migration

```
    def self.up
            create_table :categories do |table|
                    table.column :title, :string
                    table.column :parent_id, :integer
                    table.column :category_id, :integer
            end
    end

    def self.down
            drop_table :categories
    end

```

end"

\_002\_categories.close

system(“rake db:migrate VERSION=0”)  
system(“rake db:migrate”)

system(“script/generate scaffold feed straw”)

##### start the WEBrick server

puts “@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@”  
puts “\t\t\t[http://localhost:3000/feeds](http://localhost:3000/feeds)”  
puts “@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@”

system(“script/server”)  
thufir@arrakis ~/rubyCode $  
thufir@arrakis ~/rubyCode $ cat /etc/gentoo-release  
Gentoo Base System release 1.12.9  
thufir@arrakis ~/rubyCode $  
thufir@arrakis ~/rubyCode $ date  
Tue Nov 27 14:35:44 PST 2007  
thufir@arrakis ~/rubyCode $

thanks,

Thufir

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [November 28, 2007, 1:13am UTC](https://www.ruby-forum.com/t/generating-crud-interface-automagically/121439/2 "2007-11-28T01:13:35Z")

</div>

I’m not entirely sure why it’s broken, but this appears to be cause,  
however I’m not clear as to what the solution is:

thufir@arrakis ~/rubyCode $  
thufir@arrakis ~/rubyCode $ cat straw/config/routes.rb  
ActionController::Routing::Routes.draw do |map|

# The priority is based upon order of creation: first created -\>

highest priority.

# Sample of regular route:

# map.connect ‘products/:id’, :controller =\> ‘catalog’, :action =\>

‘view’

# Keep in mind you can assign values other than :controller

and :action

# Sample of named route:

# map.purchase ‘products/:id/purchase’, :controller =\>

‘catalog’, :action =\> ‘purchase’

# This route can be invoked with purchase\_url(:id =\> product.id)

# You can have the root of your site routed by hooking up ‘’

# – just remember to delete public/index.html.

# map.connect ‘’, :controller =\> “welcome”

# Allow downloading Web Service WSDL as a file with an extension

# instead of a file named ‘wsdl’

map.connect ‘:controller/service.wsdl’, :action =\> ‘wsdl’

# Install the default route as the lowest priority.

map.connect ‘:controller/:action/:id.:format’  
map.connect ‘:controller/:action/:id’  
end  
thufir@arrakis ~/rubyCode $  
thufir@arrakis ~/rubyCode $ date  
Tue Nov 27 16:11:03 PST 2007  
thufir@arrakis ~/rubyCode $

thanks,

Thufir

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [November 28, 2007, 1:37am UTC](https://www.ruby-forum.com/t/generating-crud-interface-automagically/121439/3 "2007-11-28T01:37:34Z")

</div>

Rails doesn’t “automagically” make an interface for you. Perhaps what  
you’re looking for is something like "script/generate scaffold   
" "script/generate scaffold\_resource " or  
ActiveScaffold.

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [November 28, 2007, 2:26am UTC](https://www.ruby-forum.com/t/generating-crud-interface-automagically/121439/4 "2007-11-28T02:26:52Z")

</div>

Thufir wrote:

> When I browse to [http://localhost:3000/feeds](http://localhost:3000/feeds) I expect rails to  
> “automagically” present a CRUD interface because I’ve done this with a  
> previous rails (tutorial) project. It’s like I’m missing a rake  
> command,  
> or a scaffold or migration command. I think that what _presents_ the  
> view would be an action? The action uses a controller?
> 
> However, I’m not sure and that’s a bit of a shot in the dark.

The _rails_ command makes your new rails project in a folder.

CD into your new project (a folder rails\_apps) and do a scaffold  
command, for each model and controller you want, which does the CRUD of  
model, controller, and the views. 🙂

don’t forget the database table for a model (if you are doing this)  
should have a field called ‘id’ and is auto incrementened and a primary  
key field.

some of these other dudes are better at this then me. read what they  
write.

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [November 28, 2007, 6:07am UTC](https://www.ruby-forum.com/t/generating-crud-interface-automagically/121439/5 "2007-11-28T06:07:44Z")

</div>

On Nov 27, 4:34 pm, Ryan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Rails doesn’t “automagically” make an interface for you. Perhaps what  
> you’re looking for is something like “script/generate scaffold   
> ” “script/generate scaffold\_resource ” or ActiveScaffold.

According to the screenshots and tutorial at \<http://  
[developer.apple.com/tools/rubyonrails.html](http://developer.apple.com/tools/rubyonrails.html)\> it does create a CRUD  
interface (at least that’s my take on it).

Thank you for the scaffold information, I’ll try it 🙂

-Thufir

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [November 28, 2007, 7:02am UTC](https://www.ruby-forum.com/t/generating-crud-interface-automagically/121439/6 "2007-11-28T07:02:04Z")

</div>

You can also add:

scaffold :model

to the top of the controller (:model being the name of the model you  
want the crud interface for). I believe this is the magic you are  
referring to. Just be aware that this form of scaffolding is going away  
in rails 2.0 in favor of the scaffold generator the others have posted.

-Bill

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [November 28, 2007, 8:26am UTC](https://www.ruby-forum.com/t/generating-crud-interface-automagically/121439/7 "2007-11-28T08:26:14Z")

</div>

That was really frustrating, but now it works. I don’t fully  
understand it, but at least it gives me the expected results 🙂

I can go to [http://localhost:3000/feeds](http://localhost:3000/feeds) and enter data into the  
feeds table, and go to [http://localhost:3000/categories](http://localhost:3000/categories) and enter  
data into the categories table! I have no idea how it really works,  
nor do I have any idea as to how to do inheritance, mixins, one-to-  
many relationships, nor much of anything else.

For me, the default interface is fine for now 🙂

For the record I just had my scaffold generation off:

thufir@arrakis ~/rubyCode $  
thufir@arrakis ~/rubyCode $ cat straw.rb  
require ‘fileutils’  
require “mysql”

dbname = “straw\_development”  
m = Mysql.new(“localhost”, “root”, “”)  
r = m.query(“DROP DATABASE IF EXISTS #{dbname}”)  
r = m.query(“CREATE DATABASE #{dbname}”)  
m.select\_db(dbname)  
m.query(“CREATE TABLE dummy  
(  
id integer)”)  
m.close

FileUtils.rmtree ‘straw’  
system(“rails straw”)  
FileUtils.cd(‘straw’, :verbose =\> true)

################################################

FileUtils.rm ‘config/database.yml’, :force =\> true  
database\_yml = File.open(‘config/database.yml’, ‘w’)  
database\_yml.puts "  
development:  
adapter: mysql  
database: straw\_development  
username: root  
password:  
host: localhost  
socket: /var/run/mysqld/mysqld.sock  
test:  
adapter: mysql  
database: straw\_test  
username: root  
password:  
host: localhost  
socket: /var/run/mysqld/mysqld.sock  
production:  
adapter: mysql  
database: straw\_production  
username: root  
password:  
host: localhost  
socket: /var/run/mysqld/mysqld.sock"

database\_yml.close

##############################################

system(“script/generate migration feeds”)  
system(“script/generate migration categories”)

##################################################

FileUtils.rm ‘db/migrate/001\_feeds.rb’, :force =\> true  
\_001\_feeds = File.open(‘db/migrate/001\_feeds.rb’, ‘w’)  
\_001\_feeds.puts "  
class Feeds \< ActiveRecord::Migration

```
    def self.up
            create_table :feeds do |table|
                    table.column :title, :string
                    table.column :location, :string
                    table.column :category_id, :integer
            end
    end

    def self.down
            drop_table :feeds
    end

```

end"  
\_001\_feeds.close

################################################

FileUtils.rm ‘db/migrate/002\_categories.rb’, :force =\> true  
\_002\_categories = File.open(‘db/migrate/002\_categories.rb’, ‘w’)  
\_002\_categories.puts "  
class Categories \< ActiveRecord::Migration  
def self.up  
create\_table :categories do |table|  
table.column :parent\_id, integer  
table.column :title, text  
end  
end

```
    def self.down
            drop_table :categories
    end

```

end"  
\_002\_categories.close

########################################3

system(“rake db:migrate VERSION=0”)  
system(“rake db:migrate”)

##########################################

system(“script/generate scaffold feed”)  
system(“script/generate scaffold category”)

##### start the WEBrick server

puts “@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@”  
puts “\t\t\t[http://localhost:3000/feeds](http://localhost:3000/feeds)”  
puts “@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@”

system(“script/server”)  
thufir@arrakis ~/rubyCode $

thanks,

Thufir

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [November 28, 2007, 4:35pm UTC](https://www.ruby-forum.com/t/generating-crud-interface-automagically/121439/8 "2007-11-28T16:35:45Z")

</div>

On Wed, 28 Nov 2007 11:04:23 +1030, Ryan wrote:

> Rails doesn’t “automagically” make an interface for you. Perhaps what  
> you’re looking for is something like "script/generate scaffold   
> " "script/generate scaffold\_resource " or  
> ActiveScaffold.

What I ended up doing was "script/generate scaffold " once for  
each model (I have two tables). This gives CRUD access to the flat  
tables. I suppose that "script/generate scaffold "  
is used for relationships?

thanks,

Thufir

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [November 28, 2007, 8:41am UTC](https://www.ruby-forum.com/t/generating-crud-interface-automagically/121439/9 "2007-11-28T08:41:26Z")

</div>

I fixed the scaffold command(s), phew!

pretty neat, a decent interface for data entry. I’m kinda torn, I  
want to do some one-to-many relations, see how polymorphism works in  
rails, etc, etc. Another part of me wants to work on what was the  
impetus for this whole thing: an RSS aggregator.

In any event, here’s what I have, and I can say that it works:

thufir@arrakis ~/rubyCode $  
thufir@arrakis ~/rubyCode $ cat straw.rb  
require ‘fileutils’  
require “mysql”

dbname = “straw\_development”  
m = Mysql.new(“localhost”, “root”, “”)  
r = m.query(“DROP DATABASE IF EXISTS #{dbname}”)  
r = m.query(“CREATE DATABASE #{dbname}”)  
m.select\_db(dbname)  
m.query(“CREATE TABLE dummy  
(  
id integer)”)  
m.close

FileUtils.rmtree ‘straw’  
system(“rails straw”)  
FileUtils.cd(‘straw’, :verbose =\> true)

################################################

FileUtils.rm ‘config/database.yml’, :force =\> true  
database\_yml = File.open(‘config/database.yml’, ‘w’)  
database\_yml.puts "  
development:  
adapter: mysql  
database: straw\_development  
username: root  
password:  
host: localhost  
socket: /var/run/mysqld/mysqld.sock  
test:  
adapter: mysql  
database: straw\_test  
username: root  
password:  
host: localhost  
socket: /var/run/mysqld/mysqld.sock  
production:  
adapter: mysql  
database: straw\_production  
username: root  
password:  
host: localhost  
socket: /var/run/mysqld/mysqld.sock"

database\_yml.close

##############################################

system(“script/generate migration feeds”)  
system(“script/generate migration categories”)

##################################################

FileUtils.rm ‘db/migrate/001\_feeds.rb’, :force =\> true  
\_001\_feeds = File.open(‘db/migrate/001\_feeds.rb’, ‘w’)  
\_001\_feeds.puts "  
class Feeds \< ActiveRecord::Migration

```
    def self.up
            create_table :feeds do |table|
                    table.column :title, :string
                    table.column :location, :string
                    table.column :category_id, :integer
            end
    end

    def self.down
            drop_table :feeds
    end

```

end"  
\_001\_feeds.close

################################################

FileUtils.rm ‘db/migrate/002\_categories.rb’, :force =\> true  
\_002\_categories = File.open(‘db/migrate/002\_categories.rb’, ‘w’)  
\_002\_categories.puts "  
class Categories \< ActiveRecord::Migration  
def self.up  
create\_table :categories do |table|  
table.column :prnt\_num, :integer  
table.column :title, :string  
end  
end

```
    def self.down
            drop_table :categories
    end

```

end"  
\_002\_categories.close

########################################3

system(“rake db:migrate VERSION=0”)  
system(“rake db:migrate”)

##########################################

system(“script/generate scaffold feed”)  
system(“script/generate scaffold category”)

##### start the WEBrick server

puts “@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@”  
puts “\t\t\thttp://localhost:3000/feeds”  
puts “\t\t\thttp://localhost:3000/categories”  
puts “@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@”

system(“script/server”)  
thufir@arrakis ~/rubyCode $

thanks,

Thufir
