1. Starting Application Development
Cool. We have setup a web server and a database within an hour. 10x productivity, you bet!
Ok, so now we can get started on customizing our app. I am not going to go deep into specific rails concepts such as naming the db tables in plural and model and controller singular and so on since all other resources pound on them.
User
Ok, so now we can get started on customizing our app. I am not going to go deep into specific rails concepts such as naming the db tables in plural and model and controller singular and so on since all other resources pound on them.
User
- We have already created a "users" table in MYSQL. Here is the schema:
id - primary key, unsigned, auto_increment, not null
username - varchar(20)
password - varchar (10)
email - varchar (50)
created_at - datetime, not null
updated_at - datetime, not null
Rails will automatically maintain id, created_at and updated_at columns for us. Note that these column names are special and Rails knows what to do with them if it encounters these in your table. - Lets create the scaffolding for our User table.
cd demo; ruby script/generate scaffold User user
Note the arguments to scaffold. You need to specify both the model (the first argument) and the controller (the second argument). If you skip the controller name. the code generator generates users_controller.rb and similarly named views.
Also you can use -p (--pretend) option to scaffold code generator which will show what changes will be made, but not make them.
If you create the scaffolding before creating the database tables, the code generator errors out. So create the db tables before running the scaffolding generator. This will also test the connection to the db so check in config/database.yml to make sure you have specified the
The output should be a a whole bunch of files in various sub-directories of demo.
E:\demo>ruby script/generate scaffold User user
exists app/controllers/
exists app/helpers/
create app/views/user
exists test/functional/
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/user.rb
create test/unit/user_test.rb
create test/fixtures/users.yml
create app/views/user/_form.rhtml
create app/views/user/list.rhtml
create app/views/user/show.rhtml
create app/views/user/new.rhtml
create app/views/user/edit.rhtml
create app/controllers/user_controller.rb
create test/functional/user_controller_test.rb
create app/helpers/user_helper.rb
create app/views/layouts/user.rhtml
identical public/stylesheets/scaffold.css - Start the webserver and navigate to the user pages http://127.0.0.1:3000/user
cd demo; ruby script/server
You should see the list action, which is called by the default index action :
app/controller/user_controller.rb:
def index
list
render :action => 'list'
end
In another window, do a tail -f log/development.log to see the webserver messages. - Click on the New User link at the bottom of the page and enter the info and press Create.
Congrats. This is so cool. You are already productive.
