I'm running across the following error when I try to show, edit, delete, or add a district:
undefined method `state_path' for #<#<Class:0x007f93a9e9df88>:0x007f93af11f8d8>
I'm expecting for it to generate a country_state_path link since I have link_to 'Back', [@country, @state]
but for some reason it's only giving me state_path. If I use dot-notation instead doing @country.state I receive an nilClass error.
I'm using a Tree Structure for my models:
Country
State
District
If I enter country_state_path everything works fine. But I'd rather enter it using the model notation since that works in my State Model.
(Am I using the right terms? Please correct if not, I'm still new to Rails)
class District < ActiveRecord::Base
validates_uniqueness_of :name, scope: :state_id
before_destroy :check_for_schools
belongs_to :state
#has_many :schools, :order => 'name'
private
def check_for_schools
=begin
if schools.count > 0
self.errors[:base] << "Cannot delete district while schools exist."
return false
end
=end
end
end
class DistrictsController < ApplicationController
# Allows JSON Queries
skip_before_filter :verify_authenticity_token
before_action :set_district, only: [:show, :edit, :update, :destroy]
before_filter :load_state
# GET /districts
# GET /districts.json
def index
@districts = @state.districts.all(:order => 'name ASC')
end
# GET /districts/1
# GET /districts/1.json
def show
end
# GET /districts/new
def new
@district = @state.districts.new
end
# GET /districts/1/edit
def edit
@state.districts.find(params[:id])
end
# POST /districts
# POST /districts.json
def create
@district = @state.districts.new(district_params)
respond_to do |format|
if @district.save
format.html { redirect_to [@country,@state,@district], notice: 'District was successfully created.' }
format.json { render action: 'show', status: :created, location: @district }
else
format.html { render action: 'new' }
format.json { render json: @district.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /districts/1
# PATCH/PUT /districts/1.json
def update
respond_to do |format|
if @district.update(district_params)
format.html { redirect_to [@state, @district], notice: 'District was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @district.errors, status: :unprocessable_entity }
end
end
end
# DELETE /districts/1
# DELETE /districts/1.json
def destroy
@district = @state.district.find(params[:id])
respond_to do |format|
if @district.destroy
format.html { redirect_to @state }
format.json { head :no_content }
else
format.html { redirect_to( @state, :notice => 'Unable to delete a state that has districts.') }
format.json { render json: @district.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_district
@district = District.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def district_params
params.require(:district).permit(:name, :state_id)
end
def load_state
@state = State.find(params[:state_id])
end
end
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= @district.name %>
</p>
<p>
<strong>State:</strong>
<%= @district.state_id %>
</p>
<%= link_to 'Edit', edit_country_state_district_path(@country, @state, @district) %> |
<%= link_to 'Back', [@country, @state] %>
The line that isn't working is the "link_to 'Back', [@country, @state]
.
You'll need to manually set @country in your show/edit/destroy methods, as @country is nil.
A note, though. Generally, it's best practice to only nest two deep. So:
Country
State
State
District
I know. I know. Don't shoot the messenger. Just passing the info along.