A Ruby on Rails Journey

October 6, 2011

Nifty Authentication

Filed under: survivor — Dick @ 9:14 pm

For my survivor pool app, I need users. I’ll use nifty-generators nifty:authentication to do it. First I type

rails g nifty:authentication --help

to figure out what’s what. It recommends that I install nifty:layout first, so I do.

rails g nifty:layout

It asked me if I wanted to overwrite application.html.erb and I said “Y”. Next I install authentication

rails g nifty:authentication

When I try to start the server, it complains that I don’t have the mocha gem. Oh yeah, I probably need to do some stuff first.

bundle install
rake db:migrate

Now I can start the server and navigate to http://localhost:3000/signup and it works a treat. Before I push these changes up to heroku, I want to make a landing page so it’s not the rails default one. For now, I delete public/index.html and put

root :to => "users#new"

in config/routes.rb. The landing page will be the signup page for now. Next, push it up to heroku

git push heroku master
heroku rake db:migrate

Ugh. Heroku uses postgres and my Rails app is in sqlite3. I’ll post how I fix it, but I’ll bet it won’t be pretty

October 4, 2011

Survivor Pool Setup

Filed under: survivor — Dick @ 2:29 am

Maybe one of these days I’ll actually complete a rails app. Maybe it will be this one. Maybe not. But I hate doing anything of substance and not blogging about it. I like to go back and read what I did because I don’t do rails stuff enough to be efficient yet.

In any case, my current project is an NFL survivor pool. Pick one team each week. If they win, you survive. If they lose, you’re out. Pretty simple.

I had Ruby 1.8.6 on my Windows 7 machine and Rails 2.3.8. I wrote a good deal of the app, but for some reason I couldn’t push it up to heroku. I decided to upgrade to Ruby 1.9.2 and Rails 3.1. I installed RailsInstaller 2.0 and it was a disaster. I would try to execute commands from the command line and it was clearly looking in the old 1.8.6 directory and couldn’t find them. I’ll bet it’s not really that hard to have multiple ruby versions, but honestly I don’t care to learn. I wiped out everything; uninstall RailsInstaller, delete Ruby directories, delete Git directories. Then I reinstalled RailsInstaller and things seemed to be working better.

I executed ruby -v and got 1.9.2. Good. I executed rails -v and got “file not found”. Then I did

gem install rails

and

gem install heroku

and they installed without incident. Now rails -v returned 3.1. Good. Next, I attempted to run my app locally. It told me I needed to install Rails 2.3.8 when I executed script/server. I didn’t want to use 2.3.8, so I commented out the RAILS_GEM_VERSION line in config/environment.rb. It was set to 2.3.8 and commenting it out should force it use the latest version of Rails. There’s probably a crapload of code in the app that won’t execute using Rails 3. I don’t know and I’m not that interested to find out. So I opted to start from scratch in Rails 3. That sounds like a big cop-out and it is. But the code will be far less crappy, as code always is when you write it the second time. So I

rails new dkusr2

to make a new app called dksur2. I like to use nifty-generators, so I added

gem 'nifty-generators', :group => :development

to my Gemfile and, back at the command line, ran

bundle install

It gave me a bunch of “Using” statements, which I think means it’s already installed, and one “Installing nifty-generators (0.4.6)” statement, which seems to be a good sign.

The next step was to get the empty app on git.

dick@DICK-HPELITE /c/sites/dksur2
$ git init
Initialized empty Git repository in c:/sites/dksur2/.git/

dick@DICK-HPELITE /c/sites/dksur2 (master)
$ git add .

dick@DICK-HPELITE /c/sites/dksur2 (master)
$ git commit -m "dksurvivor2"
[master (root-commit) 595bbd0] dksurvivor2
37 files changed, 1160 insertions(+), 0 deletions(-)
create mode 100644 .gitignore
create mode 100644 Gemfile
create mode 100644 Gemfile.lock
create mode 100644 README
create mode 100644 Rakefile
create mode 100644 app/assets/images/rails.png
create mode 100644 app/assets/javascripts/application.js
create mode 100644 app/assets/stylesheets/application.css
create mode 100644 app/controllers/application_controller.rb
create mode 100644 app/helpers/application_helper.rb
create mode 100644 app/mailers/.gitkeep
create mode 100644 app/models/.gitkeep
create mode 100644 app/views/layouts/application.html.erb
create mode 100644 config.ru
create mode 100644 config/application.rb
create mode 100644 config/boot.rb
create mode 100644 config/database.yml
create mode 100644 config/environment.rb
create mode 100644 config/environments/development.rb
create mode 100644 config/environments/production.rb
create mode 100644 config/environments/test.rb
create mode 100644 config/initializers/backtrace_silencers.rb
create mode 100644 config/initializers/inflections.rb
create mode 100644 config/initializers/mime_types.rb
create mode 100644 config/initializers/secret_token.rb
create mode 100644 config/initializers/session_store.rb
create mode 100644 config/initializers/wrap_parameters.rb
create mode 100644 config/locales/en.yml
create mode 100644 config/routes.rb
create mode 100644 db/seeds.rb
create mode 100644 doc/README_FOR_APP
create mode 100644 lib/assets/.gitkeep
create mode 100644 lib/tasks/.gitkeep
create mode 100644 log/.gitkeep
create mode 100644 public/404.html
create mode 100644 public/422.html
create mode 100644 public/500.html
create mode 100644 public/favicon.ico
create mode 100644 public/index.html
create mode 100644 public/robots.txt
create mode 100644 script/rails
create mode 100644 test/fixtures/.gitkeep
create mode 100644 test/functional/.gitkeep
create mode 100644 test/integration/.gitkeep
create mode 100644 test/performance/browsing_test.rb
create mode 100644 test/test_helper.rb
create mode 100644 test/unit/.gitkeep
create mode 100644 vendor/assets/stylesheets/.gitkeep
create mode 100644 vendor/plugins/.gitkeep

Wow, something worked as I expected it. Next I created a new heroku app.

dick@DICK-HPELITE /c/sites/dksur2 (master)
$ heroku create
Creating evening-snow-8011....... done, stack is bamboo-mri-1.9.2
http://evening-snow-8011.heroku.com/ | git@heroku.com:evening-snow-8011.git
Git remote heroku added

dick@DICK-HPELITE /c/sites/dksur2 (master)
$ git push heroku master
Enter passphrase for key '/c/Users/dick/.ssh/id_rsa':
Counting objects: 63, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (47/47), done.
Writing objects: 100% (63/63), 25.07 KiB, done.
Total 63 (delta 2), reused 0 (delta 0)

and a whole bunch of other lines. I went to http://evening-snow-8011.heroku.com/ and got the always heart-warming Welcome Aboard message that means I didn’t screw anything up.

My next step will be to watch Authentication from Scratch and Authentication in Rails 3.1 so I can make my User model and controllers and such. From there, I expect the app will generally look like:

Team
:name, :string

has_many :picks
has_many :users, :through => picks

Game
:start, :datetime
:weekno, :integer
:away_id, :integer
:home_id, :integer
:awayscore, :integer
:homescore, :integer

has_one :away, from team on away_id = id
has_one :home, from team on home_id = id
has_many :picks

Pick
:user_id, :integer
:team_id, :integer
:game_id, :integer

belongs_to user, team, game

June 14, 2010

Fixing Up Teams

Filed under: TenthHole — Dick @ 1:30 am

After creating the team object, there are a couple of things I want to do. First, I want to make sure a name is entered when a team is created. I fire up Intype (my favorite rails text editor), open the project, and open team.rb under the models folder. I edit it to look like this

class Team < ActiveRecord::Base
	validates_presence_of :name
end

Now if I try to make a team but don't type in a name, I get this:

Next, in Intype, I open teams_controller.rb and note that @team = Team.find(params[:id]) appears a number of times, namely in the Show, Edit, Update, and Destroy defs. To DRY that up (don't repeat yourself), I'll put that in a before filter. The top of my teams_controller.rb now looks like this:

class TeamsController  [:show, :edit, :update, :destroy]

  def index
    @teams = Team.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @teams }
    end
  end

That says before Show, Edit, Update, or Destroy are run, run a method called find_team. That new method goes at the bottom of teams_controller.rb

  def destroy
    @team = Team.find(params[:id])
    @team.destroy

    respond_to do |format|
      format.html { redirect_to(teams_url) }
      format.xml  { head :ok }
    end
  end

private
  def find_team
    @team = Team.find(params[:id])
  end

end

Now if I ever need to change that code, I can change it in one place rather than four.

Setting Up Teams

Filed under: TenthHole — Dick @ 1:11 am

The first object I’m going to create is a Team object. It only has one property: name. I opened a DOS window, navigated to my app’s directory and typee:

C:\Ruby186\tenthhole>ruby script/generate scaffold Team name:string
      exists  app/models/
      exists  app/controllers/
      exists  app/helpers/
      create  app/views/teams
      exists  app/views/layouts/
      exists  test/functional/
      exists  test/unit/
      create  test/unit/helpers/
      exists  public/stylesheets/
      create  app/views/teams/index.html.erb
      create  app/views/teams/show.html.erb
      create  app/views/teams/new.html.erb
      create  app/views/teams/edit.html.erb
      create  app/views/layouts/teams.html.erb
      create  public/stylesheets/scaffold.css
      create  app/controllers/teams_controller.rb
      create  test/functional/teams_controller_test.rb
      create  app/helpers/teams_helper.rb
      create  test/unit/helpers/teams_helper_test.rb
       route  map.resources :teams
  dependency  model
      exists    app/models/
      exists    test/unit/
      exists    test/fixtures/
      create    app/models/team.rb
      create    test/unit/team_test.rb
      create    test/fixtures/teams.yml
      create    db/migrate
      create    db/migrate/20100614005533_create_teams.rb

Generating a scaffold will create default code that, while not exactly what you want, will give you working code right off the bat. In addition to the models, views, and controllers it creates, it also creates a database migration file. To get my database up to date, I have to run the migration. From the command prompt:

C:\Ruby186\tenthhole>rake db:migrate
(in C:/Ruby186/tenthhole)
==  CreateTeams: migrating =========
-- create_table(:teams)
   -> 0.0010s
==  CreateTeams: migrated (0.0030s)

Let’s see where I am now. From the command prompt, I start the web server (ruby script/server) and point my browser to http://localhost:3000.

Not much there, but no errors. Now I can start adding teams by clicking on the New Team link.

No fancy names here, just numbers. And one ‘team’ that all available substitutes will belong to. That’s it for teams. I can add, delete, and edit the teams all with the code the scaffolding generator created.

June 13, 2010

Creating the Rails Application

Filed under: TenthHole — Dick @ 11:23 pm

With everything installed, I created my new rails app. From the command line:

C:\Ruby186>rails tenthhole
      create
      create  app/controllers
      create  app/helpers
      create  app/models
      create  app/views/layouts
      create  config/environments
      create  config/initializers
      create  config/locales
      create  db
      create  doc
      create  lib
      create  lib/tasks
      create  log
      create  public/images
      create  public/javascripts
      create  public/stylesheets
      create  script/performance
      create  test/fixtures
      create  test/functional
      create  test/integration
      create  test/performance
      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_controller.rb
      create  app/helpers/application_helper.rb
      create  config/database.yml
      create  config/routes.rb
      create  config/locales/en.yml
      create  db/seeds.rb
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/new_rails_defaults.rb
      create  config/initializers/session_store.rb
      create  config/initializers/cookie_verification_secret.rb
      create  config/environment.rb
      create  config/boot.rb
      create  config/environments/production.rb
      create  config/environments/development.rb
      create  config/environments/test.rb
      create  script/about
      create  script/console
      create  script/dbconsole
      create  script/destroy
      create  script/generate
      create  script/runner
      create  script/server
      create  script/plugin
      create  script/performance/benchmarker
      create  script/performance/profiler
      create  test/test_helper.rb
      create  test/performance/browsing_test.rb
      create  public/404.html
      create  public/422.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

The syntax rails app_name tells rails to create the application with that name and set up all the directories and many of the files I will need. It puts them all in a directory with the apps name. With the basics of an app created, I’ll test it to make sure rails is working. I navigate to C:\Ruby186\tenthhole\ and type

C:\Ruby186\tenthhole>ruby script/server
=> Booting WEBrick
=> Rails 2.3.8 application starting on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2010-06-13 14:30:48] INFO  WEBrick 1.3.1
[2010-06-13 14:30:48] INFO  ruby 1.8.6 (2010-02-04) [i386-mingw32]
[2010-06-13 14:30:48] INFO  WEBrick::HTTPServer#start: pid=4984 port=3000
[2010-06-13 14:34:03] INFO  going to shutdown ...
[2010-06-13 14:34:03] INFO  WEBrick::HTTPServer#start done.

That starts the built-in web server that runs locally. In the third line from the bottom, I see that it’s using port 3000, so I open Firefox and navigate to http://localhost:3000 and see this:

That screen means rails installed correctly. Now I can start building my app. Back at the command line, I hit Control+C to exit the web server.

Installation

Filed under: TenthHole — Dick @ 11:13 pm

New computer and new attempt to make a golf league website.  I used Installing Ruby on Rails on Windows as a guide.

First, I went to RubyForge and downloaded and installed rubyinstaller-1.8.6-p398.exe. It created a directory at C:\Ruby186\.

I opened a DOS command window and navigated to that directory. Then I installed rails:

C:\Ruby186>gem install rails
Successfully installed rake-0.8.7
Successfully installed activesupport-2.3.8
Successfully installed activerecord-2.3.8
Successfully installed rack-1.1.0
Successfully installed actionpack-2.3.8
Successfully installed actionmailer-2.3.8
Successfully installed activeresource-2.3.8
Successfully installed rails-2.3.8
8 gems installed
Installing ri documentation for rake-0.8.7...
Installing ri documentation for activesupport-2.3.8...
Installing ri documentation for activerecord-2.3.8...
Installing ri documentation for rack-1.1.0...
Installing ri documentation for actionpack-2.3.8...
Installing ri documentation for actionmailer-2.3.8...
Installing ri documentation for activeresource-2.3.8...
Installing ri documentation for rails-2.3.8...
Installing RDoc documentation for rake-0.8.7...
Installing RDoc documentation for activesupport-2.3.8...
Installing RDoc documentation for activerecord-2.3.8...
Installing RDoc documentation for rack-1.1.0...
Installing RDoc documentation for actionpack-2.3.8...
Installing RDoc documentation for actionmailer-2.3.8...
Installing RDoc documentation for activeresource-2.3.8...
Installing RDoc documentation for rails-2.3.8...

Next I installed sqlite3

sqllite command line tool
sqllite dll

I unzipped them and put them in the C:\Ruby186\bin\ directory. Back to the command line, I typed:

C:\Ruby186>gem install sqlite3-ruby
Successfully installed sqlite3-ruby-1.3.0-x86-mingw32
1 gem installed
Installing ri documentation for sqlite3-ruby-1.3.0-x86-mingw32...

Enclosing class/module 'mSqlite3' for class Statement not known

No definition for libversion
Installing RDoc documentation for sqlite3-ruby-1.3.0-x86-mingw32...

Enclosing class/module 'mSqlite3' for class Statement not known

No definition for libversion

And that’s it. Ruby, rails, and sqllite3 are installed.

June 4, 2008

Create Player Model

Filed under: TenthHole — Tags: , , , , — Dick @ 3:10 am

I have a Tenthhole project and an empty MySQL database. Now let’s start putting stuff in it. First, generate a model for players

This creates some stuff, like an empty model named player.rb and, most notably, a database migration

Here’s the migration.

Then, I make the magic happen

This update MySQL, which now looks like this

Next post I’ll set up the scaffolding.

Make TenthHole Database

Filed under: TenthHole — Tags: , , , , — Dick @ 2:46 am

I’ve created a new rails project and now I need a mysql database. I open up phpMyAdmin (right click on the Instant Rails icon, Configure, Database) and create a new database named Tenthhole_development. I know what to name it because I look in the config/database.yml file.

Here’s what it looks like in phpMyAdmin

After I hit create, I have a database with no tables in it

Next post, I’ll start creating models.

Create Tenthhole Project

Filed under: TenthHole — Tags: , , , , — Dick @ 2:30 am

I’m using RadRails and Aptana Studio Community Edition (free). I’m also using Instant Rails – yeah I’m a Windows guy. I have the 2.0 Instant Rails on one computer and whatever the previous version was on another computer. I think the one on this computer is pre-2.0, but i don’t know how to tell. I tried to follow a tutorial here that uses Rails 2.0 and I ran into some problems. It could be that I just don’t know what I’m doing, but I think it’s because I’m using pre-2.0.

I’m going to create a rails app for a golf league. The league consists of ten two-man teams playing nine holes once per week. There are additional players that can sub for any team. There are two complicated calculations that I have to worry about. The first is handicap. The second is score.

Handicap is calculated as the average of the best x scores of the last y scores, where x and y are

1 of 2

2 of 3

3 of 4

3 of 5

If the player only as one score, 80% of that differential (from par) is the handicap.

Scoring goes like this: The lower handicap player (LH) from team A plays the lower handicap player from team B. There are 21 total points available per week. The LH paring plays 9 hole match play for 1 point per hole (9 points). The higher handicap (HH) paring plays 9 hole match play for 1 point per hole (9 points). Each pairing also plays adjusted stroke play (points 19 and 20). Then the two teams play adjusted stroke play on their combined score for the final point.

Here’s my preliminary try at models:

Teams
-name

Players
-name
-email

Courses
-name
-hole1par
-hole1handicap
-hole2par
-hole2handicap

-hole9handicap

Matches
-Date
-Course
-Team
-Player
-Hole
-Score

In Aptana, File > New

February 26, 2008

Datestamp the Cart

Filed under: Agile Web Development with Rails — Dick @ 1:36 am

Since the cart is just an order with a flag set and it lives in the database, I’ll need some way to clear out old orders. Visitors will create carts and abandon them and they’d live there forever if I don’t do some maintenance. I don’t know how or when I’ll do that maintenance, but I know I’ll need a date in the cart to determine how old it is. I start by adding a field to the table. In a new migration called 008_add_last_access_date.rb:

class AddLastAccessDate < ActiveRecord::Migration
  def self.up
    add_column :orders, :last_access, :datetime
  end

  def self.down
    remove_column :orders, :last_access
  end
end

Every time I change the cart, I need to update this field. In order.rb, I created a new private method

 def update_last_access
   self.last_access = DateTime.now
   self.save
 end

And I called that in both the add_product and remove_product methods. I made the method private, but I’m not sure I needed to.

Older Posts »

Theme: Silver is the New Black. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.