views:

180

answers:

2

I'm wondering if someone can address some of the issues I am having? I create a rails app:

rails myapp -d mysql
cd myapp
haml --rails .
rake db:create:all

Then I want to use a mysql client to create tables. Lets say users and customers. A customer is also a user so you have schema like this:

users
----------------
id         int, not null, primary key, auto increment
first_name varchar(50) not null
last_name  varchar(50) not null
email      varchar(50) not null unique
password   varchar(50) not null
created_at datetime not null
updated_at datetime not null

customers
----------------
id         int, not null, primary key, auto increment
user_id    int, unique
-- some other stuff that is customer specific

what rails script commands do I need to run to get model, views and controllers created and completely filled out under my rails app? I tried this:

ruby script/generate scaffold user
ruby script/generate scaffold customer

which creates the files but the models are empty:

class User < ActiveRecord::Base
end

whats the deal? Also, I want to create an administration section to manage stuff. I figured out that I need to add routes for those:

map.namespace :admin do |admin|
  admin.resources :users
  admin.resources :customers
end

what else do I need to get the administration section going? Also here are the versions of ruby/gems I am running:

ruby 1.8.6
rails 2.3.5 & 2.3.2 <- I'm using 2.3.2 because haml
  wasn't working (or some other plugin) with 2.3.5
haml 2.2.15
rspec 1.2.9 <- I saw from another thread that I might need
  this when creating an adminstration section (rspec_controller etc)
A: 

Rails is designed for database independence with all the 'creation' done via the migrations located in db/migrate.

To create the appropriate DB tables you then simply run rake db:migrate and any migrations will be executed to create the necessary DB tables.

A good place for more information is the Rails Guides which has an example application to work through.

Grant Sayer
+3  A: 

Models are supposed to be empty by default because database schema is saved into the schema.rb file and managed using migrations.

From your answer I understand you are looking for a prepackage solution to write a couple of configurations and get everything, from controller to administration cooked for you. I'm sorry, Rails doesn't offer you this feature. If you want an administration section you actually have to code it.

It includes:

  1. creating your views and templates
  2. creating your actions
  3. mapping your routes
  4. writing your tests

The scaffold only provides you a starting point but this is a starting point you should adapt and extend to your needs.

If you want the scaffold to auto-generate your initial views according to your database table, you can pass the arguments to the command line tool

ruby script/generate scaffold user name:string age:integer

But if you want to add a new field later, you'll have to write a new migration and edit your views/actions accordingly.

More information are available in the Rails Guides and Wiki.

Simone Carletti
Ok. This is good. Models are supposed to be empty and thats fine but let me clarify something: When I go to: http://localhost:3000/users/new I get "New user" and a create button but no text boxes etc. What am I doing wrong?
DJTripleThreat
I told you before. In order to get the views populated with the model attributes, you need to pass the attributes to the scaffold call.
Simone Carletti
Yes I read what you said. I just don't understand why rails can't populate the model by reading the db or schema.rb. It can do everything else why not this? I have run that command before and gotten the expected outcome, but I shouldn't have to type all of that out on the command line.
DJTripleThreat