This demonstration assumes that you have Ruby, Ruby on Rails and MySQL installed and working correctly, with the MySQL daemon currently running. For information on downloading and installing both Ruby and Rails, please see the download instructions from the Ruby on Rails website. For information on downloading and installing MySQL, please see the MySQL Download page.
Alternatively, if you are using Windows or Mac OS X, you might want to check Instant Rails or Locomotive, respectively. These packages should contain everything you need (I personally haven't tried either, though, so your mileage may vary).
Also note that these instructions are really geared towards a Linux audience who are comfortable with the command line. Changes may be required to get things working properly under Windows or Mac OS X, but hopefully, these changes should be minor.
This demonstration was inspired by the Screen Cast — "Creating a weblog in 15 minutes" .mov file.
Note: The command below assumes that your MySQL database is a fresh installation that is not being used for anything else. If this is not true, then running the command below may not be a good idea. (It may not even work if the root user of your MySQL database has already been configured with a password.) Also, you should use different passwords, ideally. Graphical tools can be used to do these steps, if desired.
If your database is already being used and already has a root user, then the following command may be better to use:$ mysql -u root -e \ "delete from mysql.user where host <> 'localhost'; \ delete from mysql.user where user <> 'root'; \ update mysql.user set password=password('r0Rt357') where user = 'root'; \ grant all on emr_development.* to 'emrdev'@'localhost' identified by 'd3v1'; \ flush privileges;"
$ mysql -u root -p -e \ "grant all on emr_development.* to 'emrdev'@'localhost' identified by 'd3v1'; \ flush privileges;"
This will generate output indicating what files and directories are being created. Everything will be located in the newly created directory emr.$ rails emr
$ cd emr $ ls -RFC
development: adapter: mysql database: emr_development username: emrdev password: d3v1 socket: /tmp/mysql.sock
$ mysql -u emrdev -pd3v1 -e \ "DROP DATABASE if exists emr_development; CREATE DATABASE emr_development; USE emr_development; CREATE TABLE patients ( id int unsigned NOT NULL auto_increment, given_names varchar(64) NOT NULL, last_name varchar(64) NOT NULL, mcp_number varchar(12) NOT NULL, PRIMARY KEY(id) );"
Again, more output will be produced indicating what files and/or directories were created. The model is generated in the file app/models/patient.rb. (It's only two lines long!)$ ./script/generate model Patient
$ cat app/models/patient.rb
$ ./script/console Loading development environment. >> pat = Patient.new >> pat.given_names = "Homer J." >> pat.last_name = "Simpson" >> pat.mcp_number = "123456789012" >> pat.save >> Patient.create(:given_names => "Marge", ?> :last_name => "Simpson", ?> :mcp_number => "210987654321") >> require 'pp' >> pp Patient.find(1) >> pp Patient.find_by_last_name("Simpson") >> pp Patient.find_all_by_last_name("Simpson") >> pat = Patient.find_by_given_names("Homer J.") >> pat.given_names = "Homer Jay" >> pat.save >> pp Patient.find(1)
$ mysql -u emrdev -pd3v1 emr_development -e 'select * from patients'
The important files generated include a controller: app/controllers/clinic_controller.rb and a collection of associated views in app/views/clinic/ directory. Observe that for many of the methods in the controller, there exist a corresponding view. Also note that the _form.rhtml partial template file is shared by the new.rhtml and edit.rhtml views, because the form is almost identical in both cases.$ ./script/generate scaffold Patient Clinic
$ ./script/server
$ clear; tail -n 0 -f log/development.log
class Patient < ActiveRecord::Base validates_presence_of :given_names, :last_name validates_uniqueness_of :mcp_number validates_format_of :mcp_number, :with => /^\d{12}$/, :message => "must be 12 digits" end
$ mysql -u emrdev -pd3v1 -e \ "DROP DATABASE if exists emr_development; CREATE DATABASE emr_development; USE emr_development; CREATE TABLE patients ( id int unsigned NOT NULL auto_increment, given_names varchar(64) NOT NULL, last_name varchar(64) NOT NULL, mcp_number varchar(12) NOT NULL, address1 varchar(64) NOT NULL, address2 varchar(64) NOT NULL, dob date NOT NULL, PRIMARY KEY(id) );"
Allow it to overwrite files.$ ./script/generate scaffold Patient Clinic
<%= error_messages_for 'patient' %> <!--[form:patient]--> <p><label for="patient_given_names">Given names</label><br/> <%= text_field 'patient', 'given_names' %></p> <p><label for="patient_last_name">Last name</label><br/> <%= text_field 'patient', 'last_name' %></p> <p><label for="patient_mcp_number">Mcp number</label><br/> <%= text_field 'patient', 'mcp_number' %></p> <p><label for="patient_address1">Address1</label><br/> <%= text_field 'patient', 'address1' %></p> <p><label for="patient_address2">Address2</label><br/> <%= text_field 'patient', 'address2' %></p> <p><label for="patient_dob">Dob</label><br/> <%= text_field 'patient', 'dob' %></p> <!--[eoform:patient]-->
class Patient < ActiveRecord::Base validates_presence_of :given_names, :last_name, :address1, :address2 validates_uniqueness_of :mcp_number validates_format_of :mcp_number, :with => /^\d{12}$/, :message => "must be 12 digits" validates_format_of :dob, :with => /^\d\d\d\d-\d\d-\d\d$/, :message => "must be a valid date" end
$ mysql -u emrdev -pd3v1 emr_development -e \ "CREATE TABLE charts ( id int unsigned NOT NULL auto_increment, patient_id int unsigned NOT NULL, comment varchar(255) NOT NULL, weight int(4) NOT NULL, height int(4) NOT NULL, date date NOT NULL, PRIMARY KEY(id) );" $ ./script/generate model Chart
app/models/patient.rb:
class Patient < ActiveRecord::Base validates_presence_of :given_names, :last_name, :address1, :address2 validates_uniqueness_of :mcp_number validates_format_of :mcp_number, :with => /^\d{12}$/, :message => "must be 12 digits" validates_format_of :dob, :with => /^\d\d\d\d-\d\d-\d\d$/, :message => "must be a valid date" has_many :charts end
app/models/chart.rb:
class Chart < ActiveRecord::Base belongs_to :patient end
app/views/clinic/show.rhtml:
<% for column in Patient.content_columns %> <p> <b><%= column.human_name %>:</b> <%=h @patient.send(column.name) %> </p> <% end %> <%= link_to 'Edit', :action => 'edit', :id => @patient %> | <%= link_to 'Back', :action => 'list' %> <table> <tr> <% for column in Chart.content_columns %> <th><%= column.human_name %> <% end %> <% for chart in @patient.charts %> <tr> <% for column in Chart.content_columns %> <td><%=h chart.send(column.name) %> <% end %> <% end %> </table>
Each patient should now have multiple charts which can be viewed using the Show link next to each patient.$ ./script/console < db/pop-patcharts.rb
app/views/clinic/show.rhtml:
<% for column in Patient.content_columns %> <p> <b><%= column.human_name %>:</b> <%=h @patient.send(column.name) %> </p> <% end %> <%= link_to 'Edit', :action => 'edit', :id => @patient %> | <%= link_to 'Back', :action => 'list' %> <table> <tr> <% for column in Chart.content_columns %> <th><%= column.human_name %> <% end %> <%= start_form_tag :action => 'chartcreate', :id => @patient %> <tr> <td><%= text_field 'chart', 'comment' %> <td><%= text_field 'chart', 'weight', :size => 6 %> <td><%= text_field 'chart', 'height', :size => 6 %> <td><%= text_field 'chart', 'date', :size => 20 %> <td><%= submit_tag "Create" %> <%= end_form_tag %> <% for chart in @patient.charts %> <tr> <% for column in Chart.content_columns %> <td><%=h chart.send(column.name) %> <% end %> <% end %> </table>
class ClinicController < ApplicationController def index list render :action => 'list' end def list @patient_pages, @patients = paginate :patients, :per_page => 10 end def show @patient = Patient.find(params[:id]) end def new @patient = Patient.new end def create @patient = Patient.new(params[:patient]) if @patient.save flash[:notice] = 'Patient was successfully created.' redirect_to :action => 'list' else render :action => 'new' end end def chartcreate Patient.find(params[:id]).charts.create(params[:chart]) flash[:notice] = 'Chart successfully created.' redirect_to :action => 'show', :id => params[:id] end def edit @patient = Patient.find(params[:id]) end def update @patient = Patient.find(params[:id]) if @patient.update_attributes(params[:patient]) flash[:notice] = 'Patient was successfully updated.' redirect_to :action => 'show', :id => @patient else render :action => 'edit' end end def destroy Patient.find(params[:id]).destroy redirect_to :action => 'list' end end
$ mysqladmin -u root -pr0Rt357 shutdown