diff --git a/app/controllers/schools_controller.rb b/app/controllers/schools_controller.rb
index dda3848..826426e 100644
--- a/app/controllers/schools_controller.rb
+++ b/app/controllers/schools_controller.rb
@@ -25,16 +25,17 @@ class SchoolsController < ApplicationController
# GET /schools/1/edit
def edit
+ @tournament_field = @school.tournament_id
end
# POST /schools
# POST /schools.json
def create
@school = School.new(school_params)
-
+ @tournament = Tournament.find(school_params[:tournament_id])
respond_to do |format|
if @school.save
- format.html { redirect_to @school, notice: 'School was successfully created.' }
+ format.html { redirect_to @tournament, notice: 'School was successfully created.' }
format.json { render action: 'show', status: :created, location: @school }
else
format.html { render action: 'new' }
@@ -46,9 +47,10 @@ class SchoolsController < ApplicationController
# PATCH/PUT /schools/1
# PATCH/PUT /schools/1.json
def update
+ @tournament = Tournament.find(@school.tournament_id)
respond_to do |format|
if @school.update(school_params)
- format.html { redirect_to @school, notice: 'School was successfully updated.' }
+ format.html { redirect_to @tournament, notice: 'School was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
@@ -60,9 +62,10 @@ class SchoolsController < ApplicationController
# DELETE /schools/1
# DELETE /schools/1.json
def destroy
+ @tournament = Tournament.find(@school.tournament_id)
@school.destroy
respond_to do |format|
- format.html { redirect_to schools_url }
+ format.html { redirect_to @tournament }
format.json { head :no_content }
end
end
diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb
index 9493d60..db76c55 100644
--- a/app/controllers/tournaments_controller.rb
+++ b/app/controllers/tournaments_controller.rb
@@ -11,6 +11,7 @@ class TournamentsController < ApplicationController
# GET /tournaments/1.json
def show
@schools = @tournament.schools
+ @weights = @tournament.weights
end
# GET /tournaments/new
@@ -26,7 +27,6 @@ class TournamentsController < ApplicationController
# POST /tournaments.json
def create
@tournament = Tournament.new(tournament_params)
-
respond_to do |format|
if @tournament.save
format.html { redirect_to @tournament, notice: 'Tournament was successfully created.' }
diff --git a/app/controllers/weights_controller.rb b/app/controllers/weights_controller.rb
index f55be5b..9500cd8 100644
--- a/app/controllers/weights_controller.rb
+++ b/app/controllers/weights_controller.rb
@@ -11,25 +11,31 @@ class WeightsController < ApplicationController
# GET /weights/1.json
def show
@wrestler = Wrestler.all
+ @tournament = Tournament.find(@weight.tournament_id)
end
# GET /weights/new
def new
@weight = Weight.new
+ if params[:tournament]
+ @tournament_field = params[:tournament]
+ @tournament = Tournament.find(params[:tournament])
+ end
end
# GET /weights/1/edit
def edit
+ @tournament_field = @weight.tournament_id
end
# POST /weights
# POST /weights.json
def create
@weight = Weight.new(weight_params)
-
+ @tournament = Tournament.find(weight_params[:tournament_id])
respond_to do |format|
if @weight.save
- format.html { redirect_to @weight, notice: 'Weight was successfully created.' }
+ format.html { redirect_to @tournament, notice: 'Weight was successfully created.' }
format.json { render action: 'show', status: :created, location: @weight }
else
format.html { render action: 'new' }
@@ -41,9 +47,10 @@ class WeightsController < ApplicationController
# PATCH/PUT /weights/1
# PATCH/PUT /weights/1.json
def update
+ @tournament = Tournament.find(@weight.tournament_id)
respond_to do |format|
if @weight.update(weight_params)
- format.html { redirect_to @weight, notice: 'Weight was successfully updated.' }
+ format.html { redirect_to @tournament, notice: 'Weight was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
@@ -55,9 +62,10 @@ class WeightsController < ApplicationController
# DELETE /weights/1
# DELETE /weights/1.json
def destroy
+ @tournament = Tournament.find(@weight.tournament_id)
@weight.destroy
respond_to do |format|
- format.html { redirect_to weights_url }
+ format.html { redirect_to @tournament }
format.json { head :no_content }
end
end
@@ -70,6 +78,6 @@ class WeightsController < ApplicationController
# Never trust parameters from the scary internet, only allow the white list through.
def weight_params
- params.require(:weight).permit(:max)
+ params.require(:weight).permit(:max, :tournament_id)
end
end
diff --git a/app/controllers/wrestlers_controller.rb b/app/controllers/wrestlers_controller.rb
index 945b61e..87de081 100644
--- a/app/controllers/wrestlers_controller.rb
+++ b/app/controllers/wrestlers_controller.rb
@@ -5,6 +5,7 @@ class WrestlersController < ApplicationController
# GET /wrestlers.json
def index
@wrestlers = Wrestler.all
+ #@school = School.find(@wrestler.school_id)
end
# GET /wrestlers/1
@@ -19,20 +20,30 @@ class WrestlersController < ApplicationController
@school_field = params[:school]
@school = School.find(params[:school])
end
+ if @school
+ @tournament = Tournament.find(@school.tournament_id)
+ end
+ if @tournament
+ @weight = Weight.where(tournament_id: @tournament.id)
+ else
+ @weight = Weight.all
+ end
+
end
# GET /wrestlers/1/edit
def edit
+ @school_field = @wrestler.school_id
end
# POST /wrestlers
# POST /wrestlers.json
def create
@wrestler = Wrestler.new(wrestler_params)
-
+ @school = School.find(wrestler_params[:school_id])
respond_to do |format|
if @wrestler.save
- format.html { redirect_to @wrestler, notice: 'Wrestler was successfully created.' }
+ format.html { redirect_to @school, notice: 'Wrestler was successfully created.' }
format.json { render action: 'show', status: :created, location: @wrestler }
else
format.html { render action: 'new' }
@@ -44,9 +55,10 @@ class WrestlersController < ApplicationController
# PATCH/PUT /wrestlers/1
# PATCH/PUT /wrestlers/1.json
def update
+ @school = School.find(@wrestler.school_id)
respond_to do |format|
if @wrestler.update(wrestler_params)
- format.html { redirect_to @wrestler, notice: 'Wrestler was successfully updated.' }
+ format.html { redirect_to @school, notice: 'Wrestler was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
@@ -58,9 +70,10 @@ class WrestlersController < ApplicationController
# DELETE /wrestlers/1
# DELETE /wrestlers/1.json
def destroy
+ @school = School.find(@wrestler.school_id)
@wrestler.destroy
respond_to do |format|
- format.html { redirect_to wrestlers_url }
+ format.html { redirect_to @school }
format.json { head :no_content }
end
end
diff --git a/app/models/school.rb b/app/models/school.rb
index 3e3a80a..3a89241 100644
--- a/app/models/school.rb
+++ b/app/models/school.rb
@@ -1,4 +1,4 @@
class School < ActiveRecord::Base
belongs_to :tournament
- has_many :wrestlers
+ has_many :wrestlers, dependent: :destroy
end
diff --git a/app/models/tournament.rb b/app/models/tournament.rb
index f52df0b..b9944fa 100644
--- a/app/models/tournament.rb
+++ b/app/models/tournament.rb
@@ -1,3 +1,4 @@
class Tournament < ActiveRecord::Base
- has_many :schools
+ has_many :schools, dependent: :destroy
+ has_many :weights, dependent: :destroy
end
diff --git a/app/models/weight.rb b/app/models/weight.rb
index 1178aa5..8ea7bd1 100644
--- a/app/models/weight.rb
+++ b/app/models/weight.rb
@@ -1,3 +1,4 @@
class Weight < ActiveRecord::Base
- has_many :wrestlers
+ belongs_to :tournament
+ has_many :wrestlers, dependent: :destroy
end
diff --git a/app/views/tournaments/show.html.erb b/app/views/tournaments/show.html.erb
index 62109be..6871380 100644
--- a/app/views/tournaments/show.html.erb
+++ b/app/views/tournaments/show.html.erb
@@ -54,6 +54,36 @@
<% end %>
+
+
+<% if user_signed_in? %>
+ <%= link_to "New #{@tournament.name} Weight" , "/weights/new?tournament=#{@tournament.id}" %>
+<% end %>
+
+
+
| Weight Class | ++ |
|---|---|
| <%= weight.max %> | +<%= link_to 'Show', weight, :class=>"btn" %> + <% if user_signed_in? %> + <%= link_to 'Edit', edit_weight_path(weight), :class=>"btn" %> + <%= link_to 'Destroy', weight, method: :delete, data: { confirm: 'Are you sure?' }, :class=>"btn btn-danger" %> + <% end %> + | +