rails new nebcc14 -m https://raw.github.com/RailsApps/rails-composer/master/composer.rb
This will give me a little wizard where I can choose some default options. For simplicitly I'm going to omit some things like devise (used for user authentication)
In this example I will choose the first option for everything except
- Build a starter application - 3 I want to build my own
- Unit Testing? - 2 RSpec
- Fixture replacement? - 2 - Factory Girl
- set a robots.txt ? - Doesn't matter - y
- Create a Github repository - n - I usually do this on my own
- Reduce assets logger noise - sounds good - y
- Improve error reporting - why not - y
- Use or create a project-specific rvm gemset - only if you are using rvm, i use rbenv - n
I have some libraries and settings that I always want to make so I capture those in what is called a template and I can run it
rake rails:template LOCATION=../template.rb
Let's make sure that everything is working
rails server
Able to see default rails page, know it is working
rails g scaffold tournament name:string date:date
rake db:migrate
Now I can goto /tournaments and add/edit/update/delete
Ugh, it let me create a tournament without a name.
Let's fix that. First I need to add a test.
it { should validate_presence_of :name }
And then make it pass
validates :name, presence: true
Now I can run my site again and create some tournaments
rails g scaffold division name:string tournament_id:integer
rails g scaffold team name:string coach:string phone:string email:string tournament_id:integer
rake db:migrate
Now I can make teams and divisions too, but they should be part of a tournament so let's put a dropdown list on our form
vim app/views/divisions/_form.html.erb
<%= f.collection_select, :tournament_id, Tournament.all, :id, :name,{},{:class => "form-control"} %>
and do the same for teams
Now I want to show the divisions and teams on the page for the tournament, need to add a has_many to tournaments for divisions and teams then add html to render the list
Let's add a couple of quick tests to tournament
it { should have_many :teams}
it { should have_many :divisions}
And run the tests to see them fail, now make them pass.
#dournament.rb
has_many :teams, inverse_of: :tournament
has_many :divisions, inverse_of :tournament
#division.rb
belongs_to :tournament, inverse_of: :division
#team.rb
belongs_to :tournament, inverse_of: :teams
Now that I have relation ships I can use them on my tournaments page
vim app/views/tournaments/show.html.erb
Need to put h2 headers and loop through divisions then teams. Divisions gets a link to add a team, team gets a link to edit the team
Now is a good time to fix it so that if i edit the team or division and save it I want it to take me back to the tournaments page
redirect_to @team.tournament
And now I want to be able to put my teams into divisions this is call HABTM (has and belongs to many)
To do this I first need to create the table for that I"ll use a migration
rails g migration AddDivisionsTeamsTable
class AddDivisionsTeamsTable < ActiveRecord::Migration
def change
create_table :divisions_teams do |t|
t.belongs_to :division
t.belongs_to :team
end
end
end
rake db:migrate
Table name is derived by alphabetical order of class names
Want to make the add team link work, need to make it so on new team page I can pick divisions too
<%= link_to 'add team', new_team_path %>
<%= link_to 'edit', [:edit, @team] %>
Have to remember to update the params to accept ids
{:division_ids => []}
Can make some of the display nicer
1 team or 2 teams
pluralize(@division.teams,'team')
and 3rd grade boys, 4th grade boys, and 5th grade boys
@team.divisions.map(&:name).to_sentence
Can make it a lot nicer to work with by redirecting to the tournament page after saving division and team
belongs_to :tournament
redirect_to @team.tournament