Do it with our example database to become familiar with the process.
Install what needs installing:
Note that the Ubuntu package manager is out of date for rails so we need to grab rubygems and rails from ruby.
sudo apt-get install vim-ruby ruby-full git-core sqlite3 sqlitebrowser mysql-server libsqlite3-dev sudo apt-get remove rails rubygems wget http://rubyforge.org/frs/download.php/60718/rubygems-1.3.5.tgz tar xf rubygems-*.tgz cd rubygems-*/ chmod a+x setup.rb sudo ruby setup.rb cd /usr/bin sudo ln -s gem1.8 gem cd - sudo gem update --system sudo gem install rails --include-dependencies sudo gem install sqlite3-ruby # download mysql workbench
Setup the site skeleton:
mkdir /var/webapps sudo chown `whoami`:`whoami` /var/webapps cd /var/webapps rails WhatSayYe cd WhatSayYe git init # Just so that these folders are not empty touch log/.gitignore touch doc/.gitignore touch tmp/.gitignore cat - > .gitignore << EOF log/*.log tmp/**/* doc/api doc/app EOFCreate the site scaffold
# User script/generate scaffold User email:string crypted_password:string password_salt:string persistence_token:string # has_many questions # has_many answers through results # Question script/generate scaffold Question url:string question:string user:references # belongs_to user # Answer script/generate scaffold Answer answer:string question:references # belongs_to question # Result script/generate scaffold Result user:references answer:references # Exclusively a join tableInstantiate the database:
rake db:create rake db:migrateNow establish the relationships and the label in the models:
# app/models/user.rb class User < ActiveRecord::Base has_many :questions def to_label return email end def to_s return to_label end end
# app/models/question.rb class Question < ActiveRecord::Base belongs_to :user has_many :answers has_many :results, :through => :answers def to_label return question end def to_s return to_label end end
# app/models/answer.rb class Answer < ActiveRecord::Base belongs_to :question has_many :results def count return results.length end def to_label return answer end def to_s return to_label end end
# app/models/result.rb class Result < ActiveRecord::Base belongs_to :user belongs_to :answer def to_label return "#{user} chose '#{answer}'" end def to_s return to_label end endCreate 'fixtures' of sample data
# test/fixtures/users.yml # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html coolaj86: email: coolaj86@nomail.com bretswan: email: bret.swan@nomail.com
# test/fixtures/questions.yml everything: url: "everything" question: "What is the answer to Life, the Universe, and Everything?" user: coolaj86 swallow: url: "swallow" question: "What is the average airspeed velocity of an unladen swallow?" user: bretswan
# test/fixtures/answers.yml love: answer: "to love" question: everything forty_two: answer: "42" question: everything learn: answer: "to learn" question: everything meters: answer: "11 meters per second" question: swallow miles: answer: "24 miles per hour" question: swallow species: answer: "African or European swallow?" question: swallow coconut: answer: "It's not a question of velocity! It's a simple question of weight ratios! A five ounce bird could not carry a one pound coconut." question: swallow
# test/fixtures/results.yml swallow_1: user: coolaj86 answer: species swallow_2: user: bretswan answer: coconut everything_1: user: coolaj86 answer: forty_two everything_2: user: bretswan answer: forty_twoNow it should be fair to give it a test run:
rake db:fixtures:load script/server # view at http://localhost:3000/users # view at http://localhost:3000/questions # view at http://localhost:3000/answers # view at http://localhost:3000/resultsSwitch to Active Scaffold
cd /var/webapps/WhatSayYe script/plugin install git://github.com/activescaffold/active_scaffold.git script/plugin install git://github.com/ewildgoose/render_component.git -r rails-2.3 #http://activescaffold.com/
# # This must be added to each of the following # # app/views/layouts/users.html.erb # app/views/layouts/questions.html.erb # app/views/layouts/answers.html.erb # app/views/layouts/results.html.erb <%= javascript_include_tag :defaults %> <%= active_scaffold_includes %> # # Replace your controllers like so # # app/controllers/users_controller.rb class UsersController < ApplicationController active_scaffold :user do |config| config.columns.exclude :created_at config.columns.exclude :updated_at config.columns.exclude :password_salt config.columns.exclude :crypted_password config.columns.exclude :persistence_token end end # app/controllers/questions_controller.rb class QuestionsController < ApplicationController active_scaffold :questions do |config| config.columns.exclude :created_at config.columns.exclude :updated_at end end # app/controllers/answers_controller.rb class AnswersController < ApplicationController active_scaffold :answer do |config| config.columns.exclude :created_at config.columns.exclude :updated_at end end # app/controllers/results_controller.rb class ResultsController < ApplicationController active_scaffold :result do |config| config.columns.exclude :created_at config.columns.exclude :updated_at end endWe'll cover the MySQL setup in lab.
In progress... still... * setup mysql * switch to mysql with the diagram * customize active scaffold * create jsonic web service * create 3.0 web app * switch as to admin backend * switch webapp to frontend #script/plugin install git://github.com/activescaffold/active_scaffold.git #http://railscasts.com/episodes/81-fixtures-in-rails-2-0 # Fixtures
No comments:
Post a Comment